在Python Selenium脚本中无法使用特殊字符“$”作为密码。

3

样例脚本:

# -*- 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

有人能帮我解决这个问题吗?先谢谢了。


你有使用Python 3的选项吗?一般来说这是个好主意,特别是涉及到Unicode问题时。 - Arne
@ArneRecknagel 我没有管理员用户访问系统来设置Python 3。 - Subhash Deshmukh
你确定你的脚本实际上是以UTF-8编码保存的吗? - David Ferenczy Rogožan
2个回答

4

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

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

你应该尝试删除它。

在PASSWORD字符串中,#代表字母数字字符。唯一的特殊字符是'$',它位于字符串中间。 - Subhash Deshmukh
@subhash 看起来你设置了字符串的错误最后一个符号,你能试着删除它吗?在那之后,一切都对我正常工作:`IIn [1]: PASSWORD = '#####$####'In [2]: PASSWORD.encode() Out[2]: '#####$####' ` - py_dude
是的,有一些隐藏的符号。我使用退格键将其删除了。虽然它不可见,但还是谢谢。 - Subhash Deshmukh

-1

你需要转义美元符号。

尝试:

\$

我尝试使用'$'代替'$',但仍然出现相同的错误。 - Subhash Deshmukh
@Anand 你为什么这样想?这不是一个正则表达式。 - py_dude

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接