一、准备
下载浏览器驱动 chromedriver.exe (需要和Chrome浏览器版本号对应)
下载地址: https://npm.taobao.org/mirrors/chromedriver/ 根据日期查找 notes.txt 里查看支持的版本 这个支持Chrome 95版本
下载python包:selenium
pip install selenium
二、使用步骤
1.引入库
代码如下(示例):
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
其他包及作用可以参考 https://blog.csdn.net/kyaaaa1/article/details/87534998
2.初始化浏览器
代码如下(示例):
options = webdriver.ChromeOptions()
chromedriver_path = r'C:\Users\pc\Downloads\chromedriver_win32\chromedriver.exe'
options.add_argument("start-maximized")
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_experimental_option("useAutomationExtension", False)
options.add_experimental_option('excludeSwitches', ['enable-automation'])
browser = webdriver.Chrome(options=options, executable_path=chromedriver_path)
3.模拟轨迹
根据需要移动的距离生成步长list
def get_track(distance):
track = []
current = 0
mid = distance * 4 / 5
t = 0.2
v = 1
while current < distance:
if current < mid:
a = 4
else:
a = -3
v0 = v
v = v0 + a * t
move = v0 * t + 1 / 2 * a * t * t
current += move
track.append(round(move))
return track
鼠标拖动方法
def move_to_gap(slider, tracks):
ActionChains(self.browser).click_and_hold(slider).perform()
for x in tracks:
print('移动步长:', x)
ActionChains(self.browser).move_by_offset(xoffset=x, yoffset=0).perform()
time.sleep(0.31)
ActionChains(self.browser).release().perform()
实验代码
url = 'https://www.geetest.com/show'
browser.get(url)
time.sleep(2)
browser.find_element_by_xpath('//div[@class="tab-item tab-item-1"]').click()
time.sleep(2)
browser.find_element_by_xpath('//div[@aria-label="点击按钮进行验证"]').click()
time.sleep(2)
huakuai = browser.find_element_by_xpath('//div[@class="geetest_slider_button"]')
time.sleep(3)
xoffset = input('手动测量需要移动的距离:\n')
track_list = get_track(xoffset)
move_to_gap(slider, track_list)
待完善
1.自动获取滑块所需移动的距离
|