【问题标题】:Selenium using Chrome browser - Timeout during HTTP basic authentication [Python]Selenium 使用 Chrome 浏览器 - HTTP 基本身份验证期间超时 [Python]
【发布时间】:2020-06-29 05:36:47
【问题描述】:

作为自动化的一部分,我正在尝试使用 selenium 登录到本地托管的网站。该页面提供 HTTP 基本身份验证弹出窗口,我使用以下代码发送凭据。但是,在使用调试器并逐步执行代码时,我发现 TimeOut 异常重复发生(在旁边标有注释的行处)。

详情

  • 我在 Chrome 浏览器及其对应的 chrome WebDriver 上尝试了从 79.0 到最新 84.0 的所有版本,但似乎在所有情况下都会出现此异常。
  • 操作系统 - Windows Server W2k12 虚拟机。 [在 Windows 10 上也试过]
  • Python 3.8 版

代码

import time
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys

options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
driver = webdriver.Chrome(chrome_options=options)
driver.maximize_window()
driver.get(url)
wait = WebDriverWait(driver, 10)
alert = wait.until(EC.alert_is_present()) # This line causes the time out exception
alert = driver.switch_to.alert
alert.send_keys('domain\username' + Keys.TAB + 'password')
alert.accept()
time.sleep(5)

注意

  1. 由于内部页面中的错误,我无法使用https://username:password@ipaddress:port 格式通过 URL 发送凭据,因此这迫使我将上述 selenium 方法作为我唯一的选择。
  2. firefox对应代码运行良好(针对同一目标内部网站)

可能的预感

我想知道,我是否缺少新创建的 VM 上的任何包,这对于 chrome WebDriver 的工作至关重要。例如,Firefox Gecko 驱动程序需要 Visual Studio 可再发行组件才能工作。 chrome WebDriver 是否需要任何此类等效包?

【问题讨论】:

    标签: python-3.x selenium selenium-webdriver selenium-chromedriver


    【解决方案1】:

    我不相信 Basic Auth 弹出窗口在 ChromeDriver 中显示为“警报”,因此 AFAIK 您唯一的选择是 https://username:password@ipaddress:port。有趣的是,您说您可以在 Firefox 中对弹出窗口进行编程。

    在 Chrome 78 之前,身份验证弹出窗口会显示并阻止测试脚本,您必须“手动”输入凭据(或使用更通用的“桌面窗口操作”API),但我认为这不起作用没有了。

    【讨论】:

      【解决方案2】:

      最终在这里起作用的是使用 Selenium 方式使用 send_keys() 方法丢弃,但使用其他包来完成这项工作。以下两个 sn-ps 有效。

      from selenium import webdriver
      from selenium.webdriver.common.keys import Keys
      import keyboard
      ​
      options = webdriver.ChromeOptions()
      options.add_argument('--ignore-certificate-errors')
      options.add_argument('--ignore-ssl-errors')
      driver = webdriver.Chrome(r"path_to_chromedriver.exe", options=options)
      driver.maximize_window()
      driver.get("<replace with url>")
      ​
      keyboard.write(r"<replace with username>")
      keyboard.press_and_release("tab")
      keyboard.write("<replace with password>")
      keyboard.press_and_release("tab")
      keyboard.press_and_release("enter")
      

      或者这个(之前pip install pywin32

      from selenium import webdriver
      from selenium.webdriver.common.keys import Keys
      import win32com.client 
      ​
      options = webdriver.ChromeOptions()
      options.add_argument('--ignore-certificate-errors')
      options.add_argument('--ignore-ssl-errors')
      driver = webdriver.Chrome(r"path_to_chromedriver.exe", options=options)
      driver.maximize_window()
      driver.get("<replace with url>")
      ​
      shell = win32com.client.Dispatch("WScript.Shell")  
      shell.SendKeys(r"<replace with username>")
      shell.SendKeys("{TAB}")
      shell.SendKeys("<replace with password>")
      shell.SendKeys("{TAB}")
      

      【讨论】:

        猜你喜欢
        • 2019-06-26
        • 1970-01-01
        • 2011-01-03
        • 2015-03-20
        • 2019-04-06
        • 2020-06-03
        • 1970-01-01
        • 2017-11-16
        • 1970-01-01
        相关资源
        最近更新 更多