python selenium 自动化登录页面

去掉自动化标识,绕过js,绕过ip

python 复制代码
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

# 去掉自动化标识,绕过js
option = Options()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
option.add_argument('--disable-blink-features=AutomationControlled')
# 确定用户文件 ,绕过ip (要把其他已经打开的浏览器关掉)
option.add_argument(r'--user-data-dir=C:\Users\PC\AppData\Local\Google\Chrome\User Data')
time.sleep(2)
driver = webdriver.Chrome(options = option)

图形验证码获取

python 复制代码
import ddddocr
from selenium import webdriver

# 定位到图片元素,获取验证码图片链接
imgelement = driver.find_element('xpath','/html/body/div[1]/div/div[2]/div/div[2]/div/div/div[3]/div[2]/div/div/img')  # 定位验证码图片
imgelement.screenshot('captcha.jpg')  #保存验证码截图
# 验证码识别
ocr = ddddocr.DdddOcr()
with open('captcha.jpg', 'rb') as f:
    img_bytes = f.read()
res = ocr.classification(img_bytes)
print('识别出的验证码为:' + res)
# 填写验证码
driver.find_element_by_id(yanzhengmaid).send_keys(res)

获取用户名的 动态id,并通过id定位元素

python 复制代码
#selenium定位时,发现网页的用户名和密码的id是动态变化的,但有一定的规律
nameId=driver.find_element_by_xpath("//input[starts-with(@id,169)]").get_attribute("id")  #此处是一个以169开头的动态id
driver.find_element_by_id(nameId).send_keys('yonghuming')#定位id,并输入值

passwordId=driver.find_element_by_xpath("//input[ends-with(@id,'ps-k')]").get_attribute("id")  #此处是一个以ps-k结尾的动态id
driver.find_element_by_id(passwordId).send_keys('mima')

testId=driver.find_element_by_xpath("//input[contains(@id,'test')]").get_attribute("id")  #此处是一个包含test的动态id
driver.find_element_by_id(testId).click()
相关推荐
EntyIU1 天前
mineru从安装部署到测试使用完整指南
python·ocr
志栋智能1 天前
小步快跑:从单一场景开启超自动化巡检之旅
运维·网络·人工智能·自动化
安替-AnTi1 天前
厚朴 APK 搜索接口分析
python·apk·解析·taobao
山川湖海1 天前
AI时代快速学编程语言的陷阱(以Python为例)
大数据·人工智能·python
H Journey1 天前
Supervisor 进程管理工具介绍
python·supervisor·linux 运维
春日见1 天前
5分钟入门强化学习之动态规划算法与实现
大数据·人工智能·python·算法·机器学习·计算机视觉
Agent手记1 天前
电信运营商如何用AI实现携号转网自动处理?基于实在Agent的业务自动化落地与TARS大模型解析方案
运维·人工智能·ai·自动化
DeniuHe1 天前
sklearn 中所有交叉验证数据集划分方式完整总结
人工智能·python·sklearn
DeniuHe1 天前
sklearn中不同交叉验证方法的场景适配
人工智能·python·sklearn
隐于花海,等待花开1 天前
16.Python 常用第三方库概览 深度解析
python