簡體   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