1

I'm trying to log in with python Selenium to a webpage that wants authentication credentials.
Unlike the example here my username is an email address, so it contains the '@' character (and I'm using Python).

from selenium import webdriver
driver = webdriver.Firefox()
webpage = 'http://somewhere.com/cgi-bin/dirwrap.cgi?template=template&path=news'
username = 'myname@mymailbox.com'
password = 'mypassword'
url = username + ':' + password + '@' + webpage
print(url)
driver.get(url)

output is

myname@mymailbox.com:mypassword@http://somewhere.com/cgi-bin/dirwrap.cgi?template=template&path=news
Traceback (most recent call last):
File "question.py", line 12, in <module>
  driver.get(url)
File "/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 324, in get
  self.execute(Command.GET, {'url': url})
File "/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 312, in execute
  self.error_handler.check_response(response)
File "/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
  raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: Malformed URL: myname@mymailbox.com:mypassword@http://somewhere.com/cgi-bin/dirwrap.cgi?template=template&path=news is not a valid URL.

I believe the problem is from the extra '@' character. How do I put it in without creating a malformed URL?

Ian Atkinson
  • 73
  • 1
  • 3

1 Answers1

0

you don't have a right url, it need to be http://username:password@url, but you have username:password@http://url

try this

webpage = 'somewhere.com/cgi-bin/dirwrap.cgi?template=template&path=news'
username = 'myname@mymailbox.com'
password = 'mypassword'

url = 'http://{}:{}@{}'.format(username, password, webpage)

Output

http://myname@mymailbox.com:mypassword@somewhere.com/cgi-bin/dirwrap.cgi?template=template&path=news

but i think this will cause the problems, because you have two @ better way would be to use send_keys, for example

webpage = 'http://somewhere.com/cgi-bin/dirwrap.cgi?template=template&path=news'

driver.get(url)

email_input = driver.find_element_by_xpath('email input xpath')
email_inpit.clear()
email_input.send_keys(username)

pwd_input = driver.find_element_by_xpath('password input xpath')
pwd_inpit.clear()
pwd_input.send_keys(password)

# and after this you need to click on `ok` btn
Druta Ruslan
  • 7,171
  • 2
  • 28
  • 38