硒:访问被拒绝

时间:2020-09-19 19:12:45

标签: python selenium google-chrome selenium-webdriver selenium-chromedriver

我正在尝试使用Selenium从LV网站中抓取一些数据,并在单击“登录”按钮后继续显示“拒绝访问”屏幕。我觉得这是可以防止的,因为当我手动执行相同的操作时,一切似乎都可以正常工作。奇怪的是,我需要单击两次“登录”按钮才能手动登录。

我的代码:

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

options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'chromedriver.exe')
driver.get('https://secure.louisvuitton.com/eng-gb/mylv')
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//span[@class='ucm-wrapper']")))
driver.find_element_by_xpath("//button[@class='ucm-button ucm-button--default ucm-choice__yes']").click()
driver.find_element_by_id('loginloginForm').send_keys('xxx@xxx.com')
driver.find_element_by_id ('passwordloginForm').send_keys('xxxxxx')
driver.find_element_by_id('loginSubmit_').click()

错误:

You don't have permission to access "http://secure.louisvuitton.com/eng-gb/mylv;jsessionid=xxxxxxx.front61-prd?" on this server.

是否可以使用Selenium登录并绕过此方法?

3 个答案:

答案 0 :(得分:2)

我让您的代码进行了一些调整,并按如下所示进行了测试:

  • 代码块:

    from selenium import webdriver
    driver.get('https://secure.louisvuitton.com/eng-gb/mylv')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Accept and Continue']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='loginloginForm']"))).send_keys("Mudyla@stackoverflow.com")
    driver.find_element_by_xpath("//input[@id='passwordloginForm']").send_keys('Mudyla')
    driver.find_element_by_xpath("//input[@id='loginSubmit_']").click()
    

观察

类似于您的观察,我遇到了相同的障碍,但没有得到如下结果:

AccessDenied


深潜

登录上的click()确实发生了。但是,在检查网页DOM Tree时,您会发现某些<script>标签指向具有关键字 akam 的 JavaScripts 。强>。例如:

  • akam-sw.js install script version 1.3.3 "serviceWorker"in navigator&&"find"in[]&&function()
  • <script type="text/javascript" src="https://secure.louisvuitton.com/akam/11/7f0e2ae6" defer=""></script>
  • <noscript><img src="https://secure.louisvuitton.com/akam/11/pixel_7f0e2ae6?a=dD0xOWNjNTRjMmMxYzdmNmMwZjI0NTUwOGZmZDM5ZTQzMWQ5NjI5ZmIwJmpzPW9mZg==" style="visibility: hidden; position: absolute; left: -999px; top: -999px;" /></noscript>

这清楚地表明该网站受到Bot Manager提供的高级{@ 3}}僵尸程序检测服务的保护,并且响应被阻止


Bot Manager

根据文章Akamai

akamai_detection


结论

因此可以得出结论,检测到对数据的请求是由Bot Manager - Foundations驱动的Selenium实例执行的,并且响应被阻止。


参考文献

一些文档:


tl;博士

一些相关的讨论:

答案 1 :(得分:1)

我发布这个问题已经有一段时间了,但如果有人感兴趣,下面是我为解决问题而采取的步骤。

  1. 在十六进制编辑器中打开 chromedriver.exe,找到字符串 $cdc 并替换为相同长度的其他内容。然后保存并运行修改后的二进制文件。阅读此 answer 及其回复中的更多信息。

  2. Selenium python 代码:

options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument('--disable-blink-features=AutomationControlled')
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path='chromedriver.exe')
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
                                                                     'AppleWebKit/537.36 (KHTML, like Gecko) '
                                                                     'Chrome/85.0.4183.102 Safari/537.36'})

答案 2 :(得分:0)

对我来说,当我在启动驱动程序后添加以下行时它起作用了:

 driver.manage().deleteAllCookies();