“This is the 27th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

Before we start this case, let’s talk about how the selenum module works.

Selenium Module Overview

The Selenium module is a browser-based automation module. In other words, Selenium lets the browser run automatically based on the code it writes.

Corresponding syntax

  • Write code based on browser automation
  • Initiate a request :get(URL)
  • Tag location: the methods of the Find family
  • Tag interaction :send_ keys(‘ XXX ‘)
  • Execute js program :excute _script(‘jsCode ‘) one forward, one back :back(),forward()
  • Close the browser :quit()

The installation of the Selenium module will not be covered here.

The login process is detailed

General idea:

  • Use Selenium to initiate a request to 12306.cn and open the login page
  • Locate the user account box, user password box, and login button according to the id value of the label
  • After entering the account number and password, click the login button
  • Perform slider validation
  • Log in successfully

Get user id input box, password input box by id
username_tag = driver.find_element_by_id('J-userName')
password_tag = driver.find_element_by_id('J-password')

Fill in your account and password
username_tag.send_keys('xxxxxxx')
time.sleep(1)
password_tag.send_keys('xxxxxxxx')

Get the login button by id
login_btn = driver.find_element_by_id('J-login')
Click the login button
login_btn.click()
Copy the code

After clicking the login button again, the 12306 server will pop up a slider verification window, which requires us to use code to simulate the browser click and slide to complete the verification. Here we use an action chain to perform a series of actions: click and hold, and drag a certain distance in the specified direction.

Action chain use

Here we introduce the use of action chain, using action chain is very simple.

  • Import ActionChains from Selenium. Webdriver import ActionChains

  • 2. Use constructor to get action chain object

    # Define action chain, click and drag
    aco = ActionChains(driver)
    
    # Click and hold
    aco.click_and_hold(span)
    
    # Displacement specifies the distance
    aco.move_by_offset(25.0).perform()
    Copy the code

Note: The.perform() method must be called for the action chain to perform the corresponding operation

The verification code slider is dragged


# Define action chain, click and drag
aco = ActionChains(driver)
# Click and hold
aco.click_and_hold(span)

#perform() Performs the action chain operation immediately
for i in range(5):
    aco.move_by_offset(25.0).perform()
    time.sleep(0.3)

# Release the action chain
aco.release()
Copy the code

To evade detection

Since 12306 recognizes browser operations using the Selenium module, we also have to add code to circumvent detection

from selenium.webdriver import ChromeOptions
chrome_options = Options()
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
driver = webdriver.Chrome(executable_path='Your Chromedriver path',chrome_options=chrome_options)
Copy the code

The complete code

from selenium import webdriver
import requests
from lxml import etree
from selenium.webdriver import Chrome
from selenium.webdriver import ChromeOptions
from selenium.webdriver import ActionChains
import time
# Achieve no visual interface
from selenium.webdriver.chrome.options import  Options


chrome_options = Options()
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
Fill in your own chromedriver installation path here
driver = webdriver.Chrome(executable_path='C:/Users/Declan/AppData/Local/Google/Chrome/Application/chromedriver',chrome_options=chrome_options)



driver.get('https://kyfw.12306.cn/otn/resources/login.html')

Get user id input box, password input box by id
username_tag = driver.find_element_by_id('J-userName')
password_tag = driver.find_element_by_id('J-password')

Fill in your account and password
username_tag.send_keys('xxxxxxxx')
time.sleep(1)
password_tag.send_keys('xxxxxx')

Get the login button by id
login_btn = driver.find_element_by_id('J-login')
Click the login button
login_btn.click()
It must be hibernated, otherwise it will be too fast to locate the slider
time.sleep(2)
span = driver.find_element_by_css_selector('.btn_slide')

# Define action chain, click and drag
aco = ActionChains(driver)
# Click and hold
aco.click_and_hold(span)

#perform() Performs the action chain operation immediately
for i in range(5):
    aco.move_by_offset(25.0).perform()
    time.sleep(0.3)

# Release the action chain
aco.release()

time.sleep(2)
ok_btn = driver.find_element_by_css_selector('.ok')
ok_btn.click()

time.sleep(5)
driver.quit()
Copy the code