[英]Selenium Chrome Webdriver not working in headless mode with profile
所以,這是我遇到麻煩的代碼:
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
您是否嘗試過使用 arg --no-sandbox? 很多人在Chrome Driver Error using Selenium: Unable to Discover Open Pages 上都通過這個論點取得了成功。
要在Headless模式下調用Chrome 配置文件,您只能使用--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
您可以在以下位置找到一些相關的詳細討論:
確保這件事:
@Test
。tearDown(){}
方法中調用driver.quit()
以優雅地關閉和銷毀WebDriver和Web Client實例。可能是因為 ChromeDriver 版本過時。 嘗試下載一個更新的並重試執行腳本。
切換到 FireFox 驅動程序。 我在 Chrome 上遇到了同樣的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.