语法错误:(unicode错误)和#x27;UnicodeScape';编解码器可以';t使用Selenium Python解码位置中的字节

语法错误:(unicode错误)和#x27;UnicodeScape';编解码器可以';t使用Selenium Python解码位置中的字节,python,selenium-webdriver,syntax-error,python-unicode,unicode-escapes,decoding,Python,Selenium Webdriver,Syntax Error,Python Unicode,Unicode Escapes,Decoding,我不确定我做错了什么 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

我不确定我做错了什么

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/")

如果你能帮助我,我将不胜感激。我想我可以给你一些建议。也许对你有帮助。我建议通过在chrome中添加一个开始选项来保存会话。为此,您需要添加一个选项,如--user data dir
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
                                                                                            
您可以在这样的地方使用代码。在这里你会找到你需要的一切! -Python中含硒的Whatsapp:。 在运行代码之前,请关闭chrome,否则Selenium将重用浏览器的当前实例,您将无法运行选项--user data dir。我希望这有帮助

此错误消息

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)
``
…表示行中有错误


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

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
使用以下任一行:

  • 使用原始前缀,即
    r
    和单引号(即
    “…”
    ):

  • 使用双引号(即,
    “…”
    )和转义反斜杠字符(即,
    \
    ):

  • 使用双引号(即,
    “…”
    )和正斜杠字符(即,
    /
    ):


工具书类 您可以在以下内容中找到一些相关的详细讨论:


这不工作Selenium.common.exceptions.WebDriverException:消息:未知错误:无法创建Chrome进程。
options.add_argument("user-data-dir=C:\Users\Username\AppData\Local\Google\Chrome\User\Data")
options.add_argument(r'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.add_argument("user-data-dir=C:/Users/Username/AppData/Local/Google/Chrome/User/Data")