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()
相关推荐
yongche_shi21 分钟前
第八十七篇:设计一个监控与告警系统
python·面试宝典·监控和告警系统
彼岸花开了吗24 分钟前
构建AI智能体:六十六、智能的边界:通过偏差-方差理论理解大模型的能力与局限
人工智能·python
适应规律36 分钟前
MMCV包的安装教程
python
CP-DD43 分钟前
训练可以正常开始 一到 Validation 就直接炸 a PTX JIT compilation failed
python·深度学习·计算机视觉
luoluoal1 小时前
基于python的des知识图谱的百科知识问答平台(源码+文档)
python·mysql·django·毕业设计
算法与编程之美1 小时前
PyTorch中torch.flatten()函数的用法
人工智能·pytorch·python·深度学习·机器学习
余衫马1 小时前
突破语言边界:Python 与 C/C++ 集成方案年度深度总结
c++·python·性能优化·年度技术总结
Justin_JGT1 小时前
flask+uwsgi+Nginx
python·nginx·flask
Biehmltym1 小时前
【AI】02实现AI Agent全栈:十分钟,跑通Python调用 Gemini(大模型)的小型Web项目
人工智能·windows·python
山海青风1 小时前
人工智能基础与应用 - 数据处理、建模与预测流程 1 : 了解人工智能
人工智能·python