簡體   English   中英

Python 上的 Selenium 與 Geckdriver 在非無頭模式時崩潰

[英]Selenium on Python with Geckdriver crashes when not in headless mode

我正在嘗試在帶有 geckodriver 的 jupyter 筆記本上使用 Selenium 。

雖然在無頭模式下它似乎工作正常,但我需要查看瀏覽器 window 因為我正在學習......

我檢查了瀏覽器和驅動程序是否兼容,驅動程序已添加到 PATH 變量中,還有一些我在周圍看到的提示...

這工作正常:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
driver.get("http://google.com/")
print ("Headless Firefox Initialized")
driver.quit()

但是當刪除無頭選項時,它會崩潰:

WebDriverException:消息:無效參數:無法終止已退出的進程

我可以在geckodriver.log中找到的唯一線索是:

1596560987355 mozrunner::runner INFO 運行命令:“/bin/firefox”“-marionette”“-foreground”“-no-remote”“-profile”“/tmp/rust_mozprofilejjh45q”錯誤:未指定DISPLAY環境變量

我已經檢查過該變量是否存在,它就像DISPLAY:0 我還嘗試更改值,將其設置為我的 ip 和端口....

這讓我有點生氣,所以如果有人能伸出我的手,那就太棒了!! 提前致謝!!

--> 更新:正如@Wunderbread 所建議的,SeleniumBase 可以正常工作。

--> 更新:我在 Jupiter Hub 上工作 Selenium。 在 jupyter 樹或 PyCharm 或 VSC 上都不起作用,但是.....從終端上的腳本完美運行......生活有奧秘...... xDD

似乎您正在刪除options.headless = True並且驅動程序正在尋找該選項。 將無頭選項設置為options.headless = False

或者寫一些無頭成為可選參數的東西,例如:

from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium import webdriver

options = FirefoxOptions()
options.add_argument("--headless")
driver = webdriver.Firefox(options=options)
driver.get("http://google.com")

更新:我建議使用 SeleniumBase,因為它為您提供了有關測試套件設置的基礎。

但是,值得注意的是,使用以下代碼,我能夠在無頭和使用瀏覽器進行測試之間切換。 我在 Mac OS 10.15.6 和 Python 3.8 上使用自制軟件滿足本地 geckodriver 要求

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.headless = False
driver = webdriver.Firefox(options=options)
driver.get("https://google.com/")
driver.quit()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM