Python – Passwords with the special character ‘$’ cannot be used in Python selenium scripts

Passwords with the special character ‘$’ cannot be used in Python selenium scripts… here is a solution to the problem.

Passwords with the special character ‘$’ cannot be used in Python selenium scripts

Sample script:

# -*- coding: utf-8 -*-
from selenium import webdriver
import os

#credentials
USERNAME = '##########'
PASSWORD = '#####$####​'

#load profile
profile = webdriver. FirefoxProfile()
profile.set_preference('browser.download.folderList', 2)  # custom location
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', os.getcwd())

# following properties to suppress download popup screen
profile.set_preference("browser.helperApps.neverAsk.openFile", "text/csv")
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/csv")

# initialise driver with above profile
driver = webdriver. Firefox(profile)

#make the request to the url
driver.get('https://api.instagram.com/oauth/authorize/?client_id=#################&redirect_uri=#############&response_type=token')
#browser.current_url
driver.implicitly_wait(5)

#Enter username and password
Username = driver.find_element_by_css_selector('#id_username')
Username.send_keys(USERNAME)

Password = driver.find_element_by_css_selector('#id_password')
Password.send_keys(PASSWORD)

#SignIn button click
SignIn = driver.find_element_by_css_selector('.button-green').click()

As you can see, I’m trying to authenticate the Instagram app. I can’t pass the correct value for a password with the special character ‘$’. I get the following error.

C:\Python27\python.exe "D:/Projects/#####/Instagram Data Extraction/ETL_Scripts/Instagram_auth.py"
Traceback (most recent call last):
  File "D:/Projects/######/Instagram Data Extraction/ETL_Scripts/Instagram_auth.py", line 35, in <module>
    Password.send_keys(PASSWORD)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 322, in send_keys
    self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': keys_to_typing(value)})
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 457, in _execute
    return self._parent.execute(command, params)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 231, in execute
    response = self.command_executor.execute(driver_command, params)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 392, in execute
    data = utils.dump_json(params)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\utils.py", line 32, in dump_json
    return json.dumps(json_struct)
  File "C:\Python27\lib\json\__init__.py", line 243, in dumps
    return _default_encoder.encode(obj)
  File "C:\Python27\lib\json\encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "C:\Python27\lib\json\encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe2 in position 0: unexpected end of data

Can anyone help me with this?
Thanks in advance.

Solution

It looks like you have a symbol at the end of the string that cannot be encoded

>>> PASSWORD.decode('utf-8')
u'#####$####\u200b'

You should try to remove it

Related Problems and Solutions