SyntaxError:unicode 错误“unicodeescape”编解码器无法使用 Selenium Python 解码位置中的字节

问题描述

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
                                                                                            

我不确定我做错了什么。

{{1}}

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

解决方法

这个错误信息...

ref()

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


理想情况下,您需要替换该行:

import { defineComponent,ref } from 'vue'

export default defineComponent({
  name: 'CardDisplay',props: {
    items: {
      type: Array,default: () => []
    },itemComponent: Object,maxItemWidth: { type: Number,default: 200 },itemRatio: { type: Number,default: 1.25 },gapSize: { type: Number,default: 50 },maxYCount: { type: Number,default: Infinity }
  },setup () {
    return {
      containerSize: ref({ width: 0,height: 0 }),count: ref({ x: 0,y: 0 }),scale: ref(0),prevScrollTimestamp: 0,scroll: ref(0),isTouched: ref(false),touchStartX: ref(0),touchCurrentX: ref(0)
    }
  },computed: {
    touchDeltaX (): number {
      return this.touchCurrentX - this.touchStartX
    }
  },...
}

使用以下任一行:

  • 使用raw前缀,即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') ):

    "..."
  • 使用双引号(即\)和正斜杠字符(即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 添加启动选项来保存会话。为此,您需要添加一个选项,如 --user-data-dir 您可以在这样的地方使用代码。在这里你会找到你需要的一切! - 在 Python 中使用 Selenium 的 Whatsapp:https://www.geeksforgeeks.org/whatsapp-using-python/。 在运行您的代码之前关闭 chrome,否则 Selenium 将重用浏览器的当前实例,您将无法运行选项 --user-data-dir 。我希望这会有所帮助!