Python Windows 身份验证用户名和密码不起作用

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

我正在尝试在提示符(给定的 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()

这个也不行。

最佳答案

当您使用 Selenium 3.4.0geckodriver v0.18.0 时,Mozilla Firefox 53.0 通过 Python 3.6.1 你可以绕过 Basic Authentication 通过在 url 本身中嵌入 usernamepassword 来弹出,如下所示。

此解决方案打开 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")

关于Python Windows 身份验证用户名和密码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45328654/

相关文章:

python - 访问一个python类的多个实例的成员函数

python - 即使loopCondition为False,requeSTLoop(loopCondition)也不会释放

python - 用切片对象替换列表的第 i 个元素

python - 为什么规范化会将未定义的字符更改为 'a' ?

java - 如何找到下拉菜单中选定的选项?

javascript - 通过selenium Python复制粘贴?

docker - 在 headless (headless) chrome Docker 中获取运行 selenium 的空页面

python - 查找标题包含文本的 anchor

selenium - CSS 和 XPath 选择器有什么区别?就跨浏览器测试的性能而言,哪个更好?

python - 如何在 Selenium 和 Python 中使用类型查找元素