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()
相关推荐
im_AMBER13 分钟前
学习日志05 python
python·学习
大虫小呓18 分钟前
Python 处理 Excel 数据 pandas 和 openpyxl 哪家强?
python·pandas
哪 吒31 分钟前
2025B卷 - 华为OD机试七日集训第5期 - 按算法分类,由易到难,循序渐进,玩转OD(Python/JS/C/C++)
python·算法·华为od·华为od机试·2025b卷
摸爬滚打李上进2 小时前
重生学AI第十六集:线性层nn.Linear
人工智能·pytorch·python·神经网络·机器学习
真智AI2 小时前
利用 Claude Opus 4 自动化 GitHub 工作流:从安装到实战详解
运维·自动化·github
凛铄linshuo3 小时前
爬虫简单实操2——以贴吧为例爬取“某吧”前10页的网页代码
爬虫·python·学习
牛客企业服务3 小时前
2025年AI面试推荐榜单,数字化招聘转型优选
人工智能·python·算法·面试·职场和发展·金融·求职招聘
胡斌附体3 小时前
linux测试端口是否可被外部访问
linux·运维·服务器·python·测试·端口测试·临时服务器
likeGhee4 小时前
python缓存装饰器实现方案
开发语言·python·缓存
项目題供诗4 小时前
黑马python(二十五)
开发语言·python