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()
相关推荐
Pocker_Spades_A4 分钟前
Python快速入门专业版(五十八)——正则表达式(re):爬虫文本提取利器(从语法到实战)
爬虫·python·正则表达式
还是鼠鼠7 分钟前
AI掘金头条新闻系统 (Toutiao News)-获取新闻分类
后端·python·mysql·fastapi·web
m0_6908258234 分钟前
c++ RAII机制详解 c++如何利用RAII管理资源
jvm·数据库·python
小郑加油38 分钟前
python学习Day13:实际应用——pandas 进阶计算
python·学习·pandas
熊猫_豆豆41 分钟前
基于真实火星探测任务的实际轨道设计(Python版)
python·天体物理·火星探测器轨迹·数学物理
RSTJ_16251 小时前
PYTHON+AI LLM DAY FOURTY-SEVEN
开发语言·人工智能·python·深度学习
爱码小白1 小时前
MySQL运维篇
大数据·数据库·python
wang3zc1 小时前
HTML函数能否用外接显卡坞提升性能_eGPU对HTML函数帮助【汇总】
jvm·数据库·python
2301_781571421 小时前
mysql如何配置自增ID预留_mysql innodb_autoinc_lock_mode参数
jvm·数据库·python
子午1 小时前
校园课堂异常行为检测系统~Python+YOLOV8算法+深度学习+模型训练+人工智能
人工智能·python·yolo