简体   繁体   中英

Opening a password protected page in Python and Selenium WebDriver

I am trying to open this local IP in my web browser.

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

when I run the above code it asks for username and password on 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

after I provide username and password nothing opens in Firefox ? and also how can I provide username and password in the code itself ? Is there any better way to do it?

Update: When I directly put the link in the browser a pop-up opens asking for username and password. Only after I provide the username & password page opens otherwise throws 401 error

What you are doing here:

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

Instead, use driver.get() method as explained in the Selenium WebDriver tutorial .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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