Python 使用Selenium访问无头Chrome中的本地存储
因此,我在Mac上运行Python中的Selenium,在使用headless Chrome时遇到了一个错误。 这是我的无头铬设置。我正在加载一个默认的chrome配置文件,它有一个扩展名:ModHeaderPython 使用Selenium访问无头Chrome中的本地存储,python,google-chrome,selenium,selenium-webdriver,Python,Google Chrome,Selenium,Selenium Webdriver,因此,我在Mac上运行Python中的Selenium,在使用headless Chrome时遇到了一个错误。 这是我的无头铬设置。我正在加载一个默认的chrome配置文件,它有一个扩展名:ModHeader driver.execute_script( "localStorage.setItem('profiles', JSON.stringify([{ " + " title: 'Selenium', hideComment: true, ap
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_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地址进行请求。因此,我访问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: [] " +
"}])); ")
当我不运行headless Chrome时不会出现的问题是,当我尝试运行driver.execute_脚本时,我遇到了以下错误
消息:将目标移出边界:未能读取“localStorage”
来自“窗口”的属性:拒绝对此文档的访问
您是否无法使用headless chrome访问本地存储?我的配置是否需要更改?有什么好主意吗 我在使用python selenium和Chrome浏览器时收到一条错误消息“加载扩展时出错”。
下面的代码将对此起作用
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")
试着截图。您尝试“移动”的位置可能不会显示。屏幕截图将帮助您诊断正在发生的事情。我只是去example.com,然后executescriptI不会移动任何地方。只是driver.get(“)time.sleep(.5)然后上面的driver.executescript我已经弄明白了。我在几个代理之间来回移动,其中一个代理是打开的代理,导致了问题。它没有正确加载example.com,因此脚本无法执行。太棒了。修复的代码是什么样子的?