【问题标题】:Accessing LocalStorage in Headless Chrome Using Selenium使用 Selenium 在 Headless Chrome 中访问 LocalStorage
【发布时间】:2018-06-20 16:53:27
【问题描述】:

所以我在 Mac 上从 Python 运行 Selenium,但在使用无头 Chrome 时遇到了错误。 这是我的无头镀铬设置。我正在加载具有一个扩展名的默认 chrome 配置文件:ModHeader

我的设置如下

 chrome_options = webdriver.ChromeOptions()
 chrome_options.add_argument('headless')
 chrome_options.add_argument("--no-sandbox")
 chrome_options.add_argument('--disable-gpu')
 chrome_options.add_argument('--proxy-server=%s' % PROXY)
 chrome_options.add_argument("user-agent={}".format(USER_AGENT))
 chrome_options.add_argument('--window-size=1420,1080')
 chrome_options.add_argument("user-data-dir=/Users/test/Library/Application Support/Google/Chrome/")
 driver = webdriver.Chrome(chrome_options=chrome_options)

我想使用带有特定 IP 地址的 ProxyMesh 进行请求。所以我访问 localstorage 以在 ModHeader 中设置正确的标头

 driver.execute_script(
  "localStorage.setItem('profiles', JSON.stringify([{                " +
     "  title: 'Selenium', hideComment: true, appendMode: '',           " +
     "  headers: [                                                      " +
     "   {enabled: true, name: 'X-ProxyMesh-IP', value: '%s', comment: ''}"%ip_address +
     "  ],                                                              " +
     "  respHeaders: [],                                                " +
     "  filters: []                                                     " +
     "}]));                                                        ")

当我不运行无头 Chrome 时不会发生的问题是,当我尝试运行 driver.execute_script 时遇到以下错误。

消息:将目标移出边界:无法读取“localStorage” 来自“窗口”的属性:拒绝访问此文档。

您无法使用无头 chrome 访问本地存储吗?我的配置有什么需要改变的吗?任何想法,将不胜感激?

【问题讨论】:

  • 尝试截屏。您尝试“移动”的位置可能不会显示。屏幕截图将帮助您诊断正在发生的事情。
  • 我只是去example.com然后执行脚本
  • 我哪儿也不去。只需 driver.get("example.com") time.sleep(.5) 然后是上面的 driver.executescript
  • 我想通了。我正在通过几个代理进行洗牌,我的一个代理是一个开放代理,并且导致了问题。它没有正确加载 example.com,因此脚本无法执行。
  • 太棒了。固定代码是什么样的?

标签: python google-chrome selenium selenium-webdriver


【解决方案1】:

我在 Chrome 浏览器中使用 python selenium 时收到错误消息“加载扩展出错”。 下面的代码将适用于此。

executable_path = 'Path/chromedriver.exe'   ---Where your Chrome driver.exe
capabilities = { 'chromeOptions':  { 'useAutomationExtension': False}}
driver = webdriver.Chrome(executable_path,desired_capabilities = capabilities)
driver.get("https://google.com")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-19
    • 2019-05-08
    • 1970-01-01
    • 2023-03-26
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多