简体   繁体   English

Selenium Chrome Webdriver 无法在带有配置文件的无头模式下工作

[英]Selenium Chrome Webdriver not working in headless mode with profile

So, this is the code I'm having troubles with:所以,这是我遇到麻烦的代码:

def scrap():
        options = webdriver.ChromeOptions();
        options.add_argument('headless');
        options.add_argument('--profile-directory=Profile 1')
        options.add_argument("--user-data-dir=C:/Users/omarl/AppData/Local/Google/Chrome/User Data/")
        options.add_argument("--remote-debugging-port=45447")
    
        options.add_argument("--disable-gpu") 
        browser = webdriver.Chrome(executable_path=r"C:\Users\omarl\OneDrive\Escritorio\chromedriver.exe", options=options)
        
        scrapURL = "https://es.wallapop.com/search?distance=30000&keywords=leggins&latitude=41.38804&longitude=2.17001&filters_source=quick_filters"
        browser.get(scrapURL)
        #...

And the error:和错误:

WebDriverException: unknown error: unable to discover open pages

I don't have any instances of chrome when I execute the script, and when I'm using it without the headless option it works fine.我在执行脚本时没有任何chrome实例,当我在没有headless选项的情况下使用它时,它工作正常。 Any idea why this is happening?知道为什么会这样吗? Please, note that I'm using the --remote-debuggin-port provided in similar questions.请注意,我正在使用类似问题中提供的--remote-debuggin-port

I'm using ChromeDriver 86.0.4240.22我正在使用ChromeDriver 86.0.4240.22

Have you tried using arg --no-sandbox?您是否尝试过使用 arg --no-sandbox? A lot of people on Chrome Driver Error using Selenium: Unable to Discover Open Pages have had success with that argument.很多人在Chrome Driver Error using Selenium: Unable to Discover Open Pages 上都通过这个论点取得了成功。

To invoke a Chrome Profile in Headless mode you can use the --user-data-dir argument only and you can safely remove --profile-directory argument as follows:要在Headless模式下调用Chrome 配置文件,您只能使用--user-data-dir参数,您可以安全地删除--profile-directory参数,如下所示:

  • Code Block:代码块:

     from selenium import webdriver options = webdriver.ChromeOptions() options.add_argument('--headless') options.add_argument('--window-size=1920,1080') # options.add_argument('--profile-directory=Profile 1') options.add_argument(r"--user-data-dir=C:\\Users\\Soma Bhattacharjee\\AppData\\Local\\Google\\Chrome\\User Data\\Default") options.add_argument("--remote-debugging-port=9222") driver = webdriver.Chrome(options=options, executable_path=r'C:\\WebDrivers\\chromedriver.exe') driver.get('https://www.google.com/') print("Chrome Headless launched")
  • Console Output:控制台输出:

     DevTools listening on ws://127.0.0.1:9222/devtools/browser/93c67c41-e125-4d12-abc0-fcf0f07a62f4 Chrome Headless launched

References参考

You can find a couple of relevant detailed discussions in:您可以在以下位置找到一些相关的详细讨论:


Additional Considerations其他注意事项

Ensure that:确保这件事:

  • Selenium is upgraded to current released Version 3.141.0 . Selenium升级到当前发布的版本 3.141.0
  • ChromeDriver is updated to current ChromeDriver v86.0 level. ChromeDriver更新到当前ChromeDriver v86.0级别。
  • Chrome is updated to current Chrome Version 86.0 level. Chrome已更新到当前的Chrome 版本 86.0级别。 (as per ChromeDriver v86.0 release notes ). (根据ChromeDriver v86.0 发行说明)。
  • Execute your @Test as non-root user.非 root用户身份执行@Test
  • Always invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully.始终在tearDown(){}方法中调用driver.quit()以优雅地关闭和销毁WebDriverWeb Client实例。

tl; tl; dr博士

ChromeDriver remote debug port reservation race conditions ChromeDriver 远程调试端口预留竞争条件

It might be because of an outdated version of ChromeDriver.可能是因为 ChromeDriver 版本过时。 Try to download a more recent one and retry executing the script.尝试下载一个更新的并重试执行脚本。

Switch to FireFox driver.切换到 FireFox 驱动程序。 I had the same problem with Chrome.我在 Chrome 上遇到了同样的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM