2

To handle windows authentication, one way to handle such scenario is by passing credentials in URL itself as shown below

driver.get('http://username:password@abc.com')

But in my case since password contains special character, like 'password@123', so now code contains double special character '@' as shown below

driver.get('http://username:password@123@abc.com')

I tried replacing '@' with '%40' using url encoder/decoder, but it's not working. I am working with python selenium, any idea how to handle such scenario?

Shoaib Akhtar
  • 1,367
  • 4
  • 15
  • 43
  • did you try backslash, it's the usual Python escape character – Calculus Apr 18 '18 at 12:14
  • You meant to say something like this password/@123? – Shoaib Akhtar Apr 18 '18 at 12:21
  • 2
    AFAIK, `http://username:password@abc.com` approach doesn't work in last browser versions... – Andersson Apr 18 '18 at 12:24
  • that's a forward slash, like this password\@123 – Calculus Apr 18 '18 at 12:30
  • @Calculus, It shows exception selenium.common.exceptions.WebDriverException: Message:....."Cannot navigate to invalid URL"} – Shoaib Akhtar Apr 18 '18 at 12:59
  • @Andersson, I tried below example on my machine and it worked with url provided there as credential does not contain special character there, but in my case credential contains special character, so I am assuming if I can handle this then it should work with my application url as well https://stackoverflow.com/questions/45328654/python-windows-authentication-username-and-password-is-not-working/45329228#45329228 – Shoaib Akhtar Apr 18 '18 at 13:03
  • If it doesn't work with `"https://%s:%s@%s/" % (quote('my username'), quote('my passord'), "abc.com")` then it's either not a [`Basic Authentication`](https://en.wikipedia.org/wiki/Basic_access_authentication), or the page is redirected. – Florent B. Apr 24 '18 at 17:52
  • It shows window authentication same as on click on this url -> https://www.engprod-charter.net/ – Shoaib Akhtar Apr 25 '18 at 08:00
  • from you last comment, the domain is redirected to `https://www.engprod-spectrum.net/`. You need to authenticate on the landing domain without any path in the URL. An other way would be to use an extension: https://gist.github.com/florentbr/25246cd9337cebc07e2bbb0b9bf0de46 – Florent B. Apr 25 '18 at 08:21
  • Could you please explain what do you meant by this "without any path in the URL"... In my case a very long URL is displayed on landing page like https://...new_url.org/adfs/ls/wia?SAMLRequest=lZLLTsMwEEX3fEXkfd5pCVaTKrRCVCpQtSkLNshJJq0lxw4ep4W%2FJ%2FSByqYS%2B3vPjM94NP........... – Shoaib Akhtar Apr 25 '18 at 09:44
  • Regarding using chrome extension, need to check how it can be done in python selenium as I am working with python. https://gist.github.com/florentbr/25246cd9337cebc07e2bbb0b9bf0de46 – Shoaib Akhtar Apr 25 '18 at 09:46
  • For an URL defined as `scheme:[//[user[:password]@]host[:port]][/path][?query][#fragment]`, Chrome will ignore the credentials if the URL contains a path or a query. – Florent B. Apr 25 '18 at 10:43
  • Getting this issue, "Cannot navigate to invalid URL". Seems some problem in arranging data here host[:port]][/path][?query] or issue with parenthesis, trying to fix – Shoaib Akhtar Apr 25 '18 at 11:38

3 Answers3

2

A simple raw string will do the job, note that I use triple quotes just for escaping any possible errors with ' and ".

driver.get(r'''http://username:password@123@abc.com''')
innicoder
  • 2,405
  • 2
  • 13
  • 28
0

Can you please try out with the below syntax:

String url =  http://username:password%40123@abc.com;
driver.get(url);

In one of the application, we have more than one @ character in the password. I'm simply escaping the @ character. For example

URL: http://username:password@#@@sample.com
Encoded_URL:  http://username:password%40%23%40@sample.com

and then try to browse the encoded URL.

driver.get(Encoded_URL);

For me, it is working fine. Please let me know if it works for you.

  • I applied this, it seems to be loading application infinitely without entering credential. My password is something like "Abc@123". So it should be like "http://username:Abc@123%40123@abc.com"? – Shoaib Akhtar Apr 27 '18 at 12:04
  • So that means now you are not getting `selenium.common.exceptions.WebDriverException: Message:....."Cannot navigate to invalid URL` error now that you were facing earlier. So if you are not getting any error and page is getting loaded that means the problem is with server-side during authentication. Server is taking time to authenticate the request. And one more thing, if your password is `Abc@123` so the URL would be `username:Abc%40123@abc.com`. Don't forget to add protocol scheme ie `http/https`. – Aditya Gaurav Apr 27 '18 at 12:17
  • And if you are not able to see the credentials that are the behavior of the browsers due to security reasons, a user can not see the credentials while making the request. – Aditya Gaurav Apr 27 '18 at 12:20
  • That I have already tried, replacing @ in password with %40 which I mentioned in last line in my question itself, it is not working. – Shoaib Akhtar Apr 27 '18 at 13:49
  • It seems to me I need to to decode url here.... driver.get(decode_fun(Encoded_URL)), need to check the function in python to perform it – Shoaib Akhtar Apr 27 '18 at 13:52
0

If your username is 'shoaib' and your password is akhtar@123 then your .get() invocation should be:

driver.get('http://shoaib:akhtar%40123@abc.com')
edixon
  • 969
  • 5
  • 13