[英]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
You can find a couple of relevant detailed discussions in:您可以在以下位置找到一些相关的详细讨论:
Ensure that:确保这件事:
@Test
as non-root user.@Test
。driver.quit()
within tearDown(){}
method to close & destroy the WebDriver and Web Client instances gracefully.tearDown(){}
方法中调用driver.quit()
以优雅地关闭和销毁WebDriver和Web Client实例。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.