无法使用带有特殊字符的密码'$';在python selenium脚本中

无法使用带有特殊字符的密码'$';在python selenium脚本中,python,selenium,utf-8,decode,python-unicode,Python,Selenium,Utf 8,Decode,Python Unicode,示例脚本: # -*- coding: utf-8 -*- from selenium import webdriver import os #credentials USERNAME = '##########' PASSWORD = '#####$####​' #load profile profile = webdriver.FirefoxProfile() profile.set_preference('browser.download.folderList', 2) # cust

示例脚本:

# -*- 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()
如您所见,我正在尝试验证Instagram应用程序。我无法传递具有特殊字符“$”的密码的正确值。我得到以下错误

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
C:\Python27\python.exe“D:/Projects/######/Instagram数据提取/ETL_Scripts/Instagram_auth.py”
回溯(最近一次呼叫最后一次):
文件“D:/Projects/#######/Instagram数据提取/ETL_Scripts/Instagram_auth.py”,第35行,in
密码。发送密钥(密码)
文件“C:\Python27\lib\site packages\selenium\webdriver\remote\webelement.py”,第322行,在send\u键中
self._execute(Command.SEND_KEYS_TO_元素,{'value':KEYS_TO_typing(value)})
文件“C:\Python27\lib\site packages\selenium\webdriver\remote\webelement.py”,第457行,在\u execute中
返回self.\u parent.execute(命令,参数)
文件“C:\Python27\lib\site packages\selenium\webdriver\remote\webdriver.py”,第231行,在execute中
响应=self.command\u executor.execute(driver\u command,params)
文件“C:\Python27\lib\site packages\selenium\webdriver\remote\remote\u connection.py”,第392行,在execute中
data=utils.dump_json(参数)
文件“C:\Python27\lib\site packages\selenium\webdriver\remote\utils.py”,第32行,在dump\u json中
返回json.dumps(json_struct)
文件“C:\Python27\lib\json\\ uuuuu init\uuuuu.py”,第243行,转储
返回默认编码器编码(obj)
文件“C:\Python27\lib\json\encoder.py”,第207行,在encode中
chunks=self.iterencode(o,\u one\u shot=True)
文件“C:\Python27\lib\json\encoder.py”,第270行,在iterencode中
返回_iterencode(o,0)
UnicodeDecodeError:“utf8”编解码器无法解码位置0中的字节0xe2:数据意外结束
有人能帮我吗?
提前谢谢。

您需要避开$sign

尝试:


看起来字符串末尾有一个无法编码的符号

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

您应该尝试删除它

您是否可以选择改用python 3?一般来说,这是一个好主意,但特别是关于unicode的问题。@ArneRecnagel我没有管理员用户访问系统来设置python 3。你确定你的脚本实际上是用UTF-8编码保存的吗?我尝试添加了“\$”而不是“$”,但仍然得到相同的错误。@Anand你为什么这么认为?它不是RegExpIn密码字符串#表示字母数字字符。只有特殊字符是'$',它位于字符串的中间。@ SuthHASH,似乎您设置了字符串的最后一个符号,可以尝试删除它吗?从那以后,一切对我来说都正常了:
IIn[1]:PASSWORD='.'密码'.'密码'.'是的,在[2]:PASSWORD.encode()Out[2]中有一些隐藏的符号。我用退格删除了它。然而,它是看不见的。谢谢
>>> PASSWORD.decode('utf-8')
u'#####$####\u200b'