【问题标题】:Selenium Chrome Webdriver not working in headless mode with profileSelenium Chrome Webdriver 无法在配置文件的无头模式下工作
【发布时间】:2020-11-10 14:55:04
【问题描述】:

所以,这是我遇到问题的代码:

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)
        #...

还有错误:

WebDriverException: unknown error: unable to discover open pages

当我执行脚本时,我没有任何 chrome 实例,当我在没有 headless 选项的情况下使用它时,它工作正常。知道为什么会这样吗?请注意,我使用的是类似问题中提供的--remote-debuggin-port

我正在使用 ChromeDriver 86.0.4240.22

【问题讨论】:

  • 对于无头版本,您是否也在使用相同的可执行路径?
  • 您的路径可能有问题。试试options.add_argument(r'--user-data-dir="C:\Users\omarl\AppData\Local\Chromium\User Data\Default"')
  • @Norhther 很难给你一个明确的答案,因为你的问题中省略了可能导致你错误的代码部分。请提供更多详细信息。

标签: python selenium selenium-chromedriver remote-debugging google-chrome-headless


【解决方案1】:

要在Headless 模式下调用Chrome Profile,您只能使用--user-data-dir 参数,您可以安全地删除--profile-directory 参数,如下所示:

  • 代码块:

    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")
    
  • 控制台输出:

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

参考文献

您可以在以下位置找到一些相关的详细讨论:


其他注意事项

确保:

  • Selenium 升级到当前发布的Version 3.141.0
  • ChromeDriver 已更新到当前的ChromeDriver v86.0 级别。
  • Chrome 已更新至当前 Chrome 版本 86.0 级别。 (根据ChromeDriver v86.0 release notes)。
  • 非root用户身份执行@Test
  • 始终在 tearDown(){} 方法中调用 driver.quit() 以优雅地关闭和销毁 WebDriverWeb Client 实例。

tl;博士

ChromeDriver remote debug port reservation race conditions

【讨论】:

  • 这里是you can use...。所以我的代码的问题是我使用了 args --profile-directory--user-data-dir?答案应该明确说明错误是什么。
【解决方案2】:

您是否尝试过使用 arg --no-sandbox? Chrome Driver Error using Selenium: Unable to Discover Open Pages 上的很多人都在这个论点上取得了成功。

【讨论】:

  • 这是个好主意,但“browser.get”之后的其他内容可能会导致他的问题。我赞成您的回答,因为这是帮助他解决问题的可靠尝试。
猜你喜欢
  • 2022-12-18
  • 2018-01-19
  • 2022-08-05
  • 1970-01-01
  • 1970-01-01
  • 2020-02-10
  • 1970-01-01
  • 2023-01-22
  • 2022-09-23
相关资源
最近更新 更多