【问题标题】:Unable to use password with special character '$' in python selenium script无法在 python selenium 脚本中使用带有特殊字符“$”的密码
【发布时间】:2018-02-08 14:31:17
【问题描述】:

示例脚本:

# -*- 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 的问题。
  • @ArneRecknagel 我没有系统管理员权限来设置 python 3。
  • 您确定您的脚本实际上是以 UTF-8 编码保存的吗?

标签: python selenium utf-8 decode python-unicode


【解决方案1】:

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

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

你应该尝试删除它

【讨论】:

  • 在 PASSWORD 字符串中 # 表示字母数字字符。只有特殊字符是字符串中间的“$”。
  • @subhash 似乎您设置了错误的字符串的最后一个符号,您可以尝试删除它吗?之后一切正常:IIn [1]: PASSWORD = '#####$####' In [2]: PASSWORD.encode() Out[2]: '#####$####'
  • 是的,有一些隐藏符号。我使用退格键删除了它。虽然它不可见。谢谢。
【解决方案2】:

您需要转义 $ 符号。

尝试:

\$

【讨论】:

  • 我尝试添加 '\$' 而不是 '$',但仍然遇到同样的错误。
  • @Anand 你为什么这么认为?这不是正则表达式
猜你喜欢
  • 2018-07-07
  • 2014-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-05
  • 2021-10-12
  • 2022-06-30
  • 2013-01-04
相关资源
最近更新 更多