selenium 成功绕过淘宝登录反爬机制

前言
selenium + webdriver 在登录淘宝时会出现反爬滑块,该滑块无论怎么滑也滑不成功,只会出现 哎呀,出错了,点击刷新再来一次

有两个问题存在,导致 selenium + webdriver 在登录时被检测出来
一:淘宝页面加载的JS中有检测selenium的,如下图:


二:window.navigator.webdriver的值为true
当我们正常打开chrome浏览器时window.navigator.webdriver的值是为undefined,

而通过chromedriver调起的chrome浏览器时window.navigator.webdriver的值是为true


针对以上两个问题 我们做出相对应措施即可绕过
一:修改chromedriver.exe
使用Notepad++ 编辑器,右击打开 chromedriver.exe ,出现一堆乱码时不要慌ctrl + f,输入$cdc,查找会看到有这么一串key值 $cdc_asdjflasutopfhvcZLmcfl_

通过key值我们可以发现该值与淘宝JS检测selenium的值一致!

之后我们把上面key值标志为红色部分的字符串进行修改替换即可。

注意 :查找的字符只有$cdc四个字符,没有空格 没有空格 没有空格
注意 :修改后的字符长度要和原来的一致!


二:设置Chrome为开发者模式

option = webdriver.ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation']) # 这里去掉window.navigator.webdriver的特性
option.add_argument("--disable-blink-features=AutomationControlled")
browser = webdriver.Chrome(executable_path="E:/Python/chromedriver.exe", options=option)


实例
就这,就这样成功绕过登录反爬啦!
ps:这里的用户和密码是随便输的哈,输入正确的用户名和密码是可以登录的!

from time import sleep

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

option = webdriver.ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation']) # 这里去掉window.navigator.webdriver的特性
option.add_argument("--disable-blink-features=AutomationControlled") # 屏蔽webdriver特征
browser = webdriver.Chrome(executable_path="E:/Python/chromedriver.exe", options=option)
browser.get('http://www.taobao.com')
browser.maximize_window()
button = WebDriverWait(browser, timeout=30).until(EC.presence_of_element_located((By.CLASS_NAME, 'h')))
button.click()
username_sender = WebDriverWait(browser, timeout=30).until(EC.presence_of_element_located((By.ID, 'fm-login-id')))
username_sender.send_keys("xxxxxx")
password_sender=WebDriverWait(browser, timeout=30).until(EC.presence_of_element_located((By.ID, 'fm-login-password')))
password_sender.send_keys("xxxxxx")
sleep(3)
try:
browser.switch_to.frame(0)
# 找到滑块
slider = browser.find_element_by_xpath("//span[contains(@class, 'btn_slide')]")
# 判断滑块是否可见
if slider.is_displayed():
# 点击并且不松开鼠标
ActionChains(browser).click_and_hold(on_element=slider).perform()
# 往右边移动258个位置
ActionChains(browser).move_by_offset(xoffset=258, yoffset=0).perform()
# 松开鼠标
ActionChains(browser).pause(0.5).release().perform()
browser.switch_to.default_content()
except:
pass
button = WebDriverWait(browser, timeout=30).until(EC.presence_of_element_located((By.CLASS_NAME, 'password-login')))
button.click()

原文地址:https://www.cnblogs.com/xiaocaicai-cc/p/14841292.html