Ankit
Ankit

Reputation: 394

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

Upvotes: 1

Views: 2113

Answers (1)

Mikko Ohtamaa
Mikko Ohtamaa

Reputation: 83768

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:[email protected]/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.

Upvotes: 1

Related Questions