繁体   English   中英

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position using Selenium Python

[英]SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position using Selenium Python

from selenium import webdriver
from time import sleep
from selenium.webdriver.chrome.options import Options
path = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(path)
options.add_argument("user-data-dir=C:\Users\Username\AppData\Local\Google\Chrome\User\Data")
driver = webdriver.Chrome(chrome_options=options) 
sleep(1)
driver.get("https://web.whatsapp.com/")

我不确定我做错了什么。

options.add_argument("user-data-dir=C:\Users\Username\AppData\Local\Google\Chrome\User\Data")
^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 16-17: truncated \UXXXXXXXX escape
                                                                                            

如果你能帮助我,我将不胜感激

此错误消息...

options.add_argument("user-data-dir=C:\Users\Username\AppData\Local\Google\Chrome\User\Data") 
^ SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 16-17: truncated \UXXXXXXXX

...表示该行中存在错误。


理想情况下,您需要更换线路:

options.add_argument("user-data-dir=C:\Users\Username\AppData\Local\Google\Chrome\User\Data")

使用以下任一行:

  • 使用原始前缀,即r单引号(即'...' ):

     options.add_argument(r'user-data-dir=C:\Users\Username\AppData\Local\Google\Chrome\User\Data')
  • 使用双引号(即"..." )和 escaping 反斜杠字符(即\ ):

     options.add_argument("user-data-dir=C:\\Users\\Username\\AppData\\Local\\Google\\Chrome\\User\\Data")
  • 使用双引号(即"..." )和正斜杠字符(即/ ):

     options.add_argument("user-data-dir=C:/Users/Username/AppData/Local/Google/Chrome/User/Data")

参考

您可以在以下位置找到一些相关的详细讨论:

options = webdriver.ChromeOptions()
options.add_argument("--disable-notifications")
options.add_argument("disable-infobars")
#path to your browser
options.binary_location = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
#path to the binary files
chrome_driver_binary = r"/Users/name/Downloads/chromedriver"
driver = webdriver.Chrome(chrome_driver_binary, options=options)
driver.get(https://www.web.whatsapp.com)
``

我想我可以给你一些想法。 也许它会帮助你。 我建议通过向 chrome 添加启动选项来保存 session。 为此,您需要添加一个选项,例如 --user-data-dir 您可以使用来自这样的地方的代码。 在这里你会找到你需要的一切:-Whatsapp 和 Python 中的 Selenium: https://www.geeksforgeeks.org/whatsapp-using-python/ 在运行代码之前关闭 chrome,否则 Selenium 将重用浏览器的当前实例,您将无法运行选项 --user-data-dir。 我希望这有帮助!

将 chromedriver 放在 C: 目录中,如下所示: 在此处输入图像描述

更改以下代码行:

driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")

或者

driver = webdriver.Chrome(executable_path=r'C:\Users\USERNAME\Desktop\FOLDER\chromedriver.exe')

如果您希望 chromedriver 从指定的文件路径加载。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX 转义错误使用 Selenium 和 Python python SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX 转义错误 Python SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX 转义 SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \\UXXXXXXXXX escape , on an image SyntaxError:(unicode错误)“ unicodeescape”编解码器无法解码位置0-1的字节:格式错误的\\ N字符转义 Tkinter:SyntaxError:(unicode 错误)“unicodeescape”编解码器无法解码位置 2-3 中的字节:截断的 \\UXXXXXXXX 转义 SyntaxError:(unicode 错误)“unicodeescape”编解码器无法解码 position 7-8 中的字节:截断 \UXXXXXXXX 转义 re.compile("[" ^ SyntaxError: (unicode error) 'unicodeescape' 编解码器无法解码位置 0-7 的字节:截断的 \\UXXXXXXXX 转义 “语法错误:(unicode 错误)'unicodeescape' 编解码器无法解码 position 2-3 中的字节:截断 \UXXXXXXXX 转义”。 (文件管理错误) SyntaxError: (unicode error) 'unicodeescape' 编解码器无法解码位置 115-116 中的字节:格式错误的 \\N 字符转义
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM