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

时间:2015-04-27 06:29:21

标签: python selenium

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

1 个答案:

答案 0 :(得分: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