Search code examples
pythonseleniumselenium-webdriverwindows-authenticationbasic-authentication

Python HTTP Basic Authentication through Selenium when username and password contains special characters


I have a very specific problem when trying to use default Selenium auth using this construction username:[email protected]
So here is an issue: if I use a password like this "''asd'asd';123asd' (with specific symbols) traceback will say that URL is incorrect. But if the password is just with numbers and letters -- everything is OK. Below is an example of a code that works correctly but for my example, I need to fill password with a lot of specific symbols and I don't have permission to change it.

import unittest
from selenium import webdriver


class LoggedTest(unittest.TestCase):
    @classmethod
    def setUp(inst):
        # create a new Chrome session """
        inst.driver = webdriver.Chrome()
        inst.driver.implicitly_wait(30)
        inst.driver.maximize_window()

        # navigate to the application home page """
        inst.driver.get("http://admin:[email protected]/basic_auth")

Solution

  • For Basic Authentication if the Password contains symbols e.g. "''asd'asd';123asd' you need to translate the string into UTF. As an example:

    username = "%21%40user" #stands for !@user
    password = "%0D%0Apass" #stands for ^&pass
    webpage = "something.url.com"
    

    Now you can use:

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