繁体   English   中英

在 Python 和 Selenium WebDriver 中打开受密码保护的页面

[英]Opening a password protected page in Python and Selenium WebDriver

我试图在我的网络浏览器中打开这个本地 IP。

   from selenium import webdriver
    driver = webdriver.Firefox()
    url = 'http://165.269.261.210/source/'
    page = urllib.urlopen(url)

当我运行上面的代码时,它要求在 Python shell 上输入用户名和密码。

Enter username for coffee at 165.269.261.210: agrawal

Warning (from warnings module):
  File "C:\Python27\Lib\getpass.py", line 92
    return fallback_getpass(prompt, stream)
GetPassWarning: Can not control echo on the terminal.
Warning: Password input may be echoed.
Enter password for agrawal.a in coffee at 165.269.261.210: bravokid

在我提供用户名和密码后,Firefox 中没有任何打开? 以及如何在代码本身中提供用户名和密码? 有没有更好的方法来做到这一点?

更新:当我直接将链接放入浏览器时,会打开一个弹出窗口,要求输入用户名和密码。 只有在我提供用户名和密码页面后才会打开,否则会引发 401 错误

你在这里做什么:

from selenium import webdriver
driver = webdriver.Firefox()
url = 'http://165.269.261.210/source/'

# This is no way connected to Selenium, but fetches URL using Python urllib library
# page = urllib.urlopen(url)
driver.get('http://username:password@165.269.261.210/source/')

相反,请按照 Selenium WebDriver 教程中的说明使用driver.get()方法。

此外,您需要在 URL 中为 HTTP Basic Auth 传递用户名和密码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM