【发布时间】: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