徐州网站开发,常用网站png,哪个云服务器便宜又好,微信如何开公众号selenium实战之模拟登录b站
基础知识铺垫#xff1a;
利用selenium进行截图#xff1a;
driver.save_screenshot() 注意图片文件名要用png结尾.
关于移动#xff1a;
ActionChains(bro).move_to_element_with_offset()# 对于某个图像ActionChains(bro).move_by_offset(…selenium实战之模拟登录b站
基础知识铺垫
利用selenium进行截图
driver.save_screenshot() 注意图片文件名要用png结尾.
关于移动
ActionChains(bro).move_to_element_with_offset()# 对于某个图像ActionChains(bro).move_by_offset()# 相对于相对位置ActionChains(bro).move_to_element() # 直接滑动到另一个位置
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
path rD:\Downloads\xx\chromedriver-win64\chromedriver.exe
driver webdriver.Chrome(executable_pathpath)
login_url https://www.baidu.com
driver.get(login_url)
driver.save_screenshot(./img/baidu.png)driver.quit()思路
登录接口
https://passport.bilibili.com/login首先需要点击密码登录找到账号的输入框输入账号找到密码输入框输入密码找到登录按钮点击登录、
其实第一步不用做因为我们发现打开这个网页默认是使用密码登录。 同样找到密码的输入框 找到登录按钮的位置 然后会弹出验证码点选择框。 注意要将这个点选择框全部包括这里的xpath只能手写因为你发现存在iframe标签。
code_tag bro.find_element_by_xpath(//div[classgeetest_panel geetest_wind]//div[classgeetest_widget])找到对应的标签后调用截图函数保存在本地交给第三方打码平台。
# #使用图鉴接口识别
result tujian.getImgCodeText(./code.png,27)#获取了识别的结果大坑
到了最重要的时候了根据第三方平台提供的坐标点选验证码下面有四种写法只有第一种和第四种是对的.
第一种
for pos in result_list:x int(pos.split(,)[0])y int(pos.split(,)[1])ActionChains(bro).move_to_element_with_offset(code_tag,x,y).click().perform()#sleep(0.5)ActionChains(bro).pause(1).perform()第二种
每一次创建的动作连都是匿名函数类型影响最小。 for pos in result_list:x int(pos.split(,)[0])y int(pos.split(,)[1])# 创建一个ActionChains实例actions ActionChains(bro)# 执行操作actions.move_to_element_with_offset(code_tag, x, y).click().perform()# 添加暂停时间actions.pause(1).perform() 错误原因每次迭代都创建了一个新的ActionChains实例并在该实例上执行操作和添加暂停时间。这样的操作会导致每次迭代都是独立的操作它们之间没有建立起连续的操作序列。
第三种
demoActionChains(bro)
for pos in result_list:x int(pos.split(,)[0])y int(pos.split(,)[1])demo.move_to_element_with_offset(code_tag,x,y).click().perform()#sleep(0.5)demo.pause(1).perform()错误原因每个perform()方法只会执行之前添加的一系列操作而不会执行之后添加的操作
在使用ActionChains时通常是在添加完所有操作后调用一次perform()来执行操作序列。不需要在每个操作后都调用perform()。
第四种
demo ActionChains(bro)for pos in result_list:x int(pos.split(,)[0])y int(pos.split(,)[1])demo.move_to_element_with_offset(code_tag, x, y).click()demo.pause(1)
# 执行所有操作
demo.perform() 我们在循环之前创建了一个单独的ActionChains实例demo。在每次迭代中我们使用该实例执行移动鼠标到指定位置并点击的操作然后添加暂停时间。这样所有的操作都被添加到了同一个ActionChains实例的操作序列中。
最后调用demo.perform()方法会执行所有的操作实现连续的验证码点击操作。
代码
from selenium import webdriver
from selenium.webdriver import ActionChains
from time import sleep
import tujian
path rD:\Downloads\xx\chromedriver-win64\chromedriver.exe
# 1.创建浏览器对象
bro webdriver.Chrome(executable_pathpath)
# 2.发起请求
login_url https://passport.bilibili.com/login
bro.get(login_url)
sleep(1)
# 3.定位到指定标签填充用户名和密码
user_box bro.find_element_by_xpath(//*[idapp]/div[2]/div[2]/div[3]/div[2]/div[1]/div[1]/input)
user_box.send_keys(username)
sleep(1)
pwd_box bro.find_element_by_xpath(//*[idapp]/div[2]/div[2]/div[3]/div[2]/div[1]/div[3]/input)
pwd_box.send_keys(mima)
sleep(1)
login_btn bro.find_element_by_xpath(//*[idapp]/div[2]/div[2]/div[3]/div[2]/div[2]/div[2])
login_btn.click()
sleep(1)# 4.定位完整的验证码对话框
# 注意在开发者工具中是可以定位到多个div表示验证码对话框的因此将这几个div都定位到以此去尝试
code_tag bro.find_element_by_xpath(//div[classgeetest_panel geetest_wind]//div[classgeetest_widget])
tagbro.find_element_by_class_name(geetest_widget)sleep(1)
print(code_tag)
print(tag)#5.识别验证码使用打码平台进行验证码识别
code_tag.screenshot(./code.png)#将验证码对话框截图保存
sleep(1)
# #使用图鉴接口识别
result tujian.getImgCodeText(./code.png,27)#获取了识别的结果
# 几个字几个坐标
# # result 154,251|145,167
print(result)
result_list result.split(|)
print(结果列表为,result_list)
# #result_list [154,251,145,167]
# #6.根据识别出验证码的结果进行处理for pos in result_list:x int(pos.split(,)[0])y int(pos.split(,)[1])ActionChains(bro).move_to_element_with_offset(code_tag,x,y).click().perform()#sleep(0.5)ActionChains(bro).pause(1).perform()#
# demo ActionChains(bro)
#
# for pos in result_list:
# x int(pos.split(,)[0])
# y int(pos.split(,)[1])
# demo.move_to_element_with_offset(code_tag, x, y).click()
# demo.pause(1)
#
# # 执行所有操作
# demo.perform()# ActionChains(bro).move_to_element_with_offset()# 对于某个图像
# ActionChains(bro).move_by_offset()# 相对于相对位置
# ActionChains(bro).move_to_element() # 直接滑动到另一个位置confirm_btn bro.find_element_by_xpath(//div[classgeetest_panel geetest_wind]//div[classgeetest_widget]/div[classgeetest_panel]/a/div)
confirm_btn.click()
sleep(50)
bro.quit()
总结
在使用ActionChains类时通常在添加完所有操作后才调用perform()方法来执行操作序列。