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()
相关推荐
国科安芯几秒前
微小卫星红外相机双MCU冗余架构的抗辐照可靠性评估
人工智能·单片机·嵌入式硬件·数码相机·架构·自动化·安全性测试
UR的出不克1 分钟前
基于 mitmproxy 的大众点评数据采集实战:自动化抓取景点与评论数据
运维·爬虫·python·自动化
程序员:钧念3 分钟前
机器学习与深度学习的区别
人工智能·python·gpt·深度学习·机器学习
蜜汁小强3 分钟前
macOS 上管理不同版本的python
开发语言·python·macos
luobinrobin6 分钟前
射频测试带界面
python
不脱发的程序猿20 分钟前
使用Python高效对比多个相似的CAN DBC数据
python·单片机·嵌入式硬件·嵌入式
winfredzhang26 分钟前
构建自动化 Node.js 项目管理工具:从文件夹监控到一键联动运行
chrome·python·sqlite·node.js·端口·运行js
AI_567829 分钟前
Airflow“3分钟上手”教程:用Python定义定时数据清洗任务
开发语言·人工智能·python
Aurora-Borealis.39 分钟前
Day 38 GPU训练和call方法
python
Ulyanov40 分钟前
PyVista三维战场仿真实战
开发语言·python·tkinter·pyvista·gui开发