![](/img/trans.png)
[英]SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape error using Selenium and 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。 我希望這有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.