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

好久不见. 提交于 2020-01-04 06:20:20

问题


I have a very specific problem when trying to use default Selenium auth using this construction username:password@siteurl.com
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:admin@theinternet.herokuapp.com/basic_auth")

回答1:


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)


来源:https://stackoverflow.com/questions/53371250/python-http-basic-authentication-through-selenium-when-username-and-password-con

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!