【问题标题】:Python Windows Authentication username and password is not workingPython Windows 身份验证用户名和密码不起作用
【发布时间】:2018-01-01 21:02:23
【问题描述】:

我正在尝试在提示符(给定 URL)中输入数据,下面的代码给了我一个错误。请帮我解决这些问题?

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Firefox()
url = "http://the-internet.herokuapp.com/basic_auth"
driver.get(url)
time.sleep(5)
alert = driver.switch_to.alert
alert.authenticate('admin','admin')
time.sleep(4)
alert.accept()

我试过了:

ActionChains(driver).send_keys("admin").send_keys(Keys.TAB).send_keys("admin").perform()

这个也不行。

【问题讨论】:

  • 不确定这是否是您的问题,但我首先看到的是您忘记了 alert = driver.switch_to.alert() 中的“()”
  • 试过了:alert = driver.switch_to.alert() TypeError: 'Alert' object is not callable

标签: python selenium selenium-webdriver windows-authentication basic-authentication


【解决方案1】:

当您与 Selenium 3.4.0geckodriver v0.18.0Mozilla Firefox 53.0Python 3.6.1 合作时您可以通过将usernamepassword 嵌入url 本身来绕过Basic Authentication 弹出窗口,如下所示。

此解决方案打开URL http://the-internet.herokuapp.com/basic_auth 并使用有效的usernamepassword 凭据进行身份验证。

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

binary = FirefoxBinary('C:\\Program Files\\Mozilla Firefox\\firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary, executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe")
driver.get("http://admin:admin@the-internet.herokuapp.com/basic_auth")

【讨论】:

  • FWIW:嵌入式凭据只能部分使用 chrome v59+,因为子资源请求将被阻止。
【解决方案2】:
    def test_1_authentication(self):
        self.driver.get("https://the-internet.herokuapp.com/basic_auth")
        shell = win32com.client.Dispatch("WScript.Shell")
        shell.Sendkeys("admin")
        time.sleep(3)
        shell.Sendkeys("{TAB}")
        time.sleep(3)
        shell.Sendkeys("admin")
        time.sleep(3)
        shell.Sendkeys("{ENTER}")
        time.sleep(3)

上面的代码也可以正常工作:)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-04-15
  • 2014-01-06
  • 2010-10-24
  • 2017-07-31
  • 2014-03-28
  • 2010-10-10
  • 1970-01-01
  • 2022-07-07
相关资源
最近更新 更多