0


selenium自动登录淘宝(设置window.navigator.webdriver为false跳过滑块)

本文章仅供学习。

一、selenium自动登录淘宝在点击登录按钮后,会出现一个滑动验证的控件,人工拖动滑动验证控件,显示验证失败,这是因为淘宝有一套反爬机制识别是否是机器自动在登录。selenium打开的浏览器的window.navigator.webdriver为true。

二、如果是人工打开浏览器登录淘宝就不会弹出滑动验证控件进行验证。正常浏览器打开的window.navigator.webdriver为false

三、代码如下:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
from selenium.webdriver import Chrome

def main():
    '''
    正常的浏览器的window.navigator.webdriver为false
    selenium自动打开的浏览器的window.navigator.webdriver为true
    淘宝反爬会识别window.navigator.webdriver为true的有滑块验证码,
    所以需要设置window.navigator.webdriver为false
    '''
    driver = Chrome()
    driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
        "source": """
        Object.defineProperty(navigator, 'webdriver', {
          get: () => false
        })
      """
    })
    url = "https://login.taobao.com/member/login.jhtml"
    driver.get(url)
    driver.implicitly_wait(10)
    driver.maximize_window()
    WebDriverWait(driver,10).until(EC.visibility_of_element_located(
        (By.ID,'fm-login-id'))).send_keys('输入自己的账号')
    driver.find_element(By.ID,'fm-login-password').send_keys('输入自己的密码')
    driver.find_element(By.CSS_SELECTOR, '.fm-button.fm-submit.password-login').click()

if __name__ == "__main__":
    main()

四、遇到问题及方案:

问题1:AttributeError: ‘WebDriver‘ object has no attribute ‘execute_cdp_cmd‘

方案:下载的selenium版本没有execute_cdp_cmd属性,需要卸载selenium重新安装

卸载selenium

pip uninstall selenium

安装清华镜像 selenium

pip install selenium==4.5.0 -i https://pypi.tuna.tsinghua.edu.cn/simple/

五、参考文档:

1、Python反反爬篇--selenium被检测到的解决办法-CSDN博客

2、Selenium实战-模拟登录淘宝并爬取商品信息_使用selenium爬取淘宝商品-CSDN博客

3、selenium4.15.2 报错Exception managing chrome: error sending request for url 和 打开Chrome浏览器自动退出问题-CSDN博客

标签: selenium python

本文转载自: https://blog.csdn.net/weixin_50971660/article/details/140095199
版权归原作者 weixin_50971660 所有, 如有侵权,请联系我们删除。

“selenium自动登录淘宝(设置window.navigator.webdriver为false跳过滑块)”的评论:

还没有评论