selenium模拟国航滑块验证码

2021-11-22 17:27:32 浏览数 (1)

本文就是大致讲解下使用selenium进行这个滑块验证码的拖动问题。

临时起意,看到了中国国际航空公司的官网,想试着采集下航班信息,发现官网是有不少难度的,操作也比较麻烦。

于是就用 fiddler 找了下国航的app接口。https://m.airchina.com.cn/ac/

于是我就直接开始采用selenium来进行页面信息采集。

我们要请求的接口是: https://m.airchina.com.cn/ac/c/invoke/qryFlights@pg 访问过去就是下图这个页面了。

当我们输入完出发地址和到达地址之后,点击查询,如图所示:

下面我们先用代码完成上述操作:

代码语言:javascript复制
from selenium import webdriver
import time
driver = webdriver.Chrome(executable_path=r'C:UserslenovoDesktopchromedriver_win32chromedriver.exe')
driver.get('https://m.airchina.com.cn/ac/c/invoke/qryFlights@pg')

driver.find_element_by_id('orgCity').click()                                     #点击出发输入框
time.sleep(1)
driver.find_element_by_xpath(r'//*[@id="airport"]').send_keys('PEK')        #发送出发城市
time.sleep(2)
driver.find_element_by_class_name('citys').click()                      #点击确认输入的城市
time.sleep(2)
driver.find_element_by_id('dstCity').click()                       #点击到达输入框
time.sleep(2)
driver.find_element_by_id('airport').send_keys('CTU')         #确认到达城市
time.sleep(2)
driver.find_element_by_class_name('citys').click()    #点击确认到达的城市
time.sleep(2)
driver.find_element_by_id('subbtn').click()     #点击查询
time.sleep(3)

下面就开始核心内容了: 首先需要导入我们ActionChains

代码语言:javascript复制
from selenium.webdriver import ActionChains

Action Chains类常用于模拟鼠标的行为,比如单击,双击,拖拽等行为

首先需要实例化,然后调用其中的方法,完成相应的操作。

代码语言:javascript复制
action = ActionChains(driver)

点击滑块并进行拖动:

代码语言:javascript复制
                action.click_and_hold(self, on_element=None).perform()
                action.move_by_offset(self, xoffset, yoffset).perform()

清楚鼠标痕迹:

代码语言:javascript复制
  action.reset_actions() 

方法其实很简单。

代码语言:javascript复制
from selenium.webdriver import ActionChains
try:
    huakuai_id = driver.find_element_by_id('dx_captcha_basic_box_1')
    if huakuai_id:
        flag = 0
        try:
            while True:
                for i in [111,148,150,165,215]:            #图像缺口位置暂时人为给出
                    time.sleep(1)
                    huakuai = driver.find_element_by_id('dx_captcha_basic_slider-img-normal_1')   #获取滑块
                    action = ActionChains(driver)                       #实例化ActionChains
                    action.click_and_hold(huakuai).perform()     #点击滑块
                    action.reset_actions()					       
                    action.move_by_offset(int(i),0).perform()       #拖动
                    time.sleep(0.7)                                                                       
                    driver.find_element_by_class_name('captcha-bg').click()  #点击空白
                    time.sleep(0.7)
                    # action.reset_actions()
                    if not huakuai_id:
                        flag = 1
                        break
                if flag == 1:
                    break
        except:
            pass
except:
    pass
time.sleep(3)
doc = driver.page_source
print(doc)

我用python的moviepy做的gif 图片有点大,分开来发为…

=。=

完整代码:

代码语言:javascript复制
from selenium import webdriver
import time
driver = webdriver.Chrome(executable_path=r'C:UserslenovoDesktopchromedriver_win32chromedriver.exe')
driver.get('https://m.airchina.com.cn/ac/c/invoke/qryFlights@pg')

driver.find_element_by_id('orgCity').click()
time.sleep(1)

driver.find_element_by_xpath(r'//*[@id="airport"]').send_keys('PEK')
time.sleep(2)
driver.find_element_by_class_name('citys').click()
time.sleep(2)
driver.find_element_by_id('dstCity').click()
time.sleep(2)
driver.find_element_by_id('airport').send_keys('CTU')
time.sleep(2)
driver.find_element_by_class_name('citys').click()
time.sleep(2)
driver.find_element_by_id('subbtn').click()
time.sleep(3)
from selenium.webdriver import ActionChains
try:
    huakuai_id = driver.find_element_by_id('dx_captcha_basic_box_1')
    if huakuai_id:
        flag = 0
        try:
            while True:
                for i in [111,148,150,165,215]:
                    time.sleep(1)
                    huakuai = driver.find_element_by_id('dx_captcha_basic_slider-img-normal_1')
                    action = ActionChains(driver)
                    action.click_and_hold(huakuai).perform()
                    action.reset_actions()
                    action.move_by_offset(int(i),0).perform()
                    time.sleep(0.7)
                    driver.find_element_by_class_name('captcha-bg').click()
                    time.sleep(0.7)
                    # action.reset_actions()
                    if not huakuai_id:
                        flag = 1
                        break
                if flag == 1:
                    break
        except:
            pass
except:
    pass
time.sleep(3)
doc = driver.page_source
print(doc)

0 人点赞