Overview of selenium modules
The selenium module is a module based on browser automation. In other words, using selenium can let the browser automatically run according to the code written by itself.
corresponding syntax
Write operational code for browser-based automation
· Initiate a request: get(url)
Label positioning: the method of find series
· Tab interaction: send_ keys('xxx')
· Execute js program: execute _script('jsCode ') to move forward and backward: back() ,forward()
· Close the browser: quit()
I won't go into details about the installation of the selenium module here.
The specific process of logging in
general idea:
· Use selenium to initiate a request to the 12306 website and open the login page
Locate the user account box, user password box and login button according to the id value of the tag
After filling in the account password, click the login button
Perform slider verification
· Successful login
# Get user account input box and password input box according to id
username_tag = driver.find_element_by_id('J-userName')
password_tag = driver.find_element_by_id('J-password')
# Fill in your own 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()
After clicking the login button again, the 12306 server will pop up the slider verification window, which requires us to use the code to simulate the browser to click and slide to complete the verification. Here we use the action chain to complete a series of operations: click and hold, drag a certain distance in the specified direction.
Action chain usage
Here we introduce the use of action chains. Using action chains is very simple.
1. Import the corresponding module from selenium.webdriver import ActionChains
2. Use the constructor to get the action chain object
# Define the action chain, click and drag
aco = ActionChains(driver)
# 点击并长按
aco.click_and_hold(span)
# 位移指定的距离
aco.move_by_offset(25,0).perform()
Note: The .perform() method must be called for the action chain to perform the corresponding operation.
Finish dragging the captcha slider
# Define the action chain, click and drag
aco = ActionChains(driver)
# 点击并长按
aco.click_and_hold(span)
#perform()立即执行动作链操作
for i in range(5):
aco.move_by_offset(25,0).perform()
time.sleep(0.3)
# 释放动作链
aco.release()
Avoid detection
Since 12306 will recognize browser actions that use selenium modules, in order to avoid the corresponding detection, we must also add the corresponding code.
from selenium.webdriver import ChromeOptions
chrome_options = Options()
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
driver = webdriver.Chrome(executable_path='你的chromedriver路径',chrome_options=chrome_options)
full 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
# 实现无可视化界面
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
# 这里填入你自己的chromedriver的安装路径
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')
# 根据id获取用户账号输入框、密码输入框
username_tag = driver.find_element_by_id('J-userName')
password_tag = driver.find_element_by_id('J-password')
# 填入自己的账号和密码
username_tag.send_keys('xxxxxxxx')
time.sleep(1)
password_tag.send_keys('xxxxxx')
# 根据id获取登录按钮
login_btn = driver.find_element_by_id('J-login')
# 点击登录按钮
login_btn.click()
# 这里必须得休眠,不然运行速度太夸,代码难以定位到滑块
time.sleep(2)
span = driver.find_element_by_css_selector('.btn_slide')
# 定义动作链,点击并拖拽
aco = ActionChains(driver)
# 点击并长按
aco.click_and_hold(span)
#perform()立即执行动作链操作
for i in range(5):
aco.move_by_offset(25,0).perform()
time.sleep(0.3)
# 释放动作链
aco.release()
time.sleep(2)
ok_btn = driver.find_element_by_css_selector('.ok')
ok_btn.click()
time.sleep(5)
driver.quit()