【问题标题】:Opening a password protected page in Python and Selenium WebDriver在 Python 和 Selenium WebDriver 中打开受密码保护的页面
【发布时间】:2015-04-27 06:29:20
【问题描述】:

我正在尝试在我的网络浏览器中打开这个本地 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 错误

【问题讨论】:

  • 所以在 python shell 上询问用户名和密码...而不是在 firefox 窗口上?
  • 是的,在页面打开之前询问。如果未提供,则会引发 401 错误
  • 你能更详细地编辑这个问题吗?用户名和密码如何影响浏览器的打开?

标签: python selenium


【解决方案1】:

你在这里做什么:

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/')

改为使用driver.get() 方法作为explained in the Selenium WebDriver tutorial

Further, you need to pass username and password in the URL for HTTP Basic Auth.

【讨论】:

  • 是的,我认为这是正确的方法。但在我的情况下,它给出了错误 IOError: [Errno http error] no host given
猜你喜欢
  • 2021-08-04
  • 1970-01-01
  • 2021-06-10
  • 2011-10-08
  • 1970-01-01
  • 2022-11-17
  • 2018-08-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多