检查到自动化测试工具,不是人在操作_验证码点选_滑动

from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options # 浏览器信息配置
import json
import requests
import time

# 可以滑动过去,后面过不去,报错,检查到自动化测试工具,不是人在操作
# 浏览器 F12 Console (控制台) window.navigator.webdriver 回车 True
# 如果你的浏览器版本是88以前,要去执行一段js代码
# web.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
# "source": """
# navigator.webdriver = undefined
# Object.defineProperty(navigator,'webdriver', {
# get: () => undefined
# })
# """
# }) # undefined 未定义,什么都没有
# 88 以后的版本用下面的方案
# 给浏览器加配置信息
opt = Options()
opt.add_argument("--disable-blink-features=AutomationControlled")
web = Chrome(options=opt)


def base64_api(img, typeid=27, uname="q6035945", pwd="q6035945"): # 用户名,密码,图片,类型
data = {"username": uname, "password": pwd, "typeid": typeid, "image": img}
result = json.loads(requests.post("http://api.ttshitu.com/predict", json=data).text)
if result['success']:
return result["data"]["result"]
else:
# !!!!!!!注意:返回 人工不足等 错误情况 请加逻辑处理防止脚本卡死 继续重新 识别
return result["message"]


web.get('https://kyfw.12306.cn/otn/resources/login.html')

web.implicitly_wait(10) # 第一件事,等待
# 切换到账号登录
web.find_element(By.XPATH, '//*[@id="toolbar_Div"]/div[2]/div[2]/ul/li[1]/a').click()
web.find_element(By.XPATH, '//*[@id="J-userName"]').send_keys('18190668010')
web.find_element(By.XPATH, '//*[@id="J-password"]').send_keys('yb18190668010')

# 找到整个验证码图片位置
img = web.find_element(By.XPATH, '//*[@id="J-log"]')
b64_verify_code = img.screenshot_as_base64 # 截屏

# 开始识别
result = base64_api(b64_verify_code)
# print(result) # 43,67|174,71

for p in result.split('|'):
x = int(p.split(',')[0])
y = int(p.split(',')[1])
ActionChains(web).move_to_element_with_offset(img, xoffset=x, yoffset=y).click().perform()
time.sleep(2)
# 点击登录按钮
web.find_element(By.XPATH, '//*[@id="J-login"]').click()

# 滑块验证
time.sleep(1)
btn = web.find_element(By.XPATH, '//*[@id="J-logi"]').click() # 找那个滑动的钮,复制cpath
ActionChains(web).drag_and_drop_by_offset(btn, xoffset=300, yoffset=0).perform() # drag 抓 yoffset=0 y轴不用动 300 300像素
# ActionChains(web).click_and_hold().move_by_offset(btn, xoffset=300, yoffset=0).perform() # click 点击 hold 保持住 和上一行代码一个意思
























posted @ 2023-08-11 22:55  严永富  阅读(20)  评论(0)    收藏  举报