简体   繁体   English

Python 上的 Selenium 与 Geckdriver 在非无头模式时崩溃

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

I'm trying to use Selenium on a jupyter notebook with geckodriver.我正在尝试在带有 geckodriver 的 jupyter 笔记本上使用 Selenium 。

Although on headless mode it seems to work fine, I need to see the browser window since I'm learning...虽然在无头模式下它似乎工作正常,但我需要查看浏览器 window 因为我正在学习......

I've checked that both browser and driver are compatibles, driver it's added on the PATH variable and a few more tips I've seen around...我检查了浏览器和驱动程序是否兼容,驱动程序已添加到 PATH 变量中,还有一些我在周围看到的提示...

This works fine:这工作正常:

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()

But when removing the headless option, it crashes:但是当删除无头选项时,它会崩溃:

WebDriverException: Message: invalid argument: can't kill an exited process WebDriverException:消息:无效参数:无法终止已退出的进程

The only clue I can find its in the geckodriver.log where I can read:我可以在geckodriver.log中找到的唯一线索是:

1596560987355 mozrunner::runner INFO Running command: "/bin/firefox" "-marionette" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofilejjh45q" Error: no DISPLAY environment variable specified 1596560987355 mozrunner::runner INFO 运行命令:“/bin/firefox”“-marionette”“-foreground”“-no-remote”“-profile”“/tmp/rust_mozprofilejjh45q”错误:未指定DISPLAY环境变量

I've checked that that variable exists and it's like DISPLAY:0 .我已经检查过该变量是否存在,它就像DISPLAY:0 I've also tried to change the value, to set it to my ip and port....我还尝试更改值,将其设置为我的 ip 和端口....

This is driving me a bit mad so if anyone can give my hand it would be awesome!!这让我有点生气,所以如果有人能伸出我的手,那就太棒了!! Thanks in advance!!提前致谢!!

--> Update: as @Wunderbread suggested, SeleniumBase works without problem. --> 更新:正如@Wunderbread 所建议的,SeleniumBase 可以正常工作。

--> Update: I was working Selenium on Jupiter Hub. --> 更新:我在 Jupiter Hub 上工作 Selenium。 Neither works on jupyter tree or PyCharm or VSC, but..... works perfect from a script on the terminal... life has mysteries.... xDD在 jupyter 树或 PyCharm 或 VSC 上都不起作用,但是.....从终端上的脚本完美运行......生活有奥秘...... xDD

It seems like you're deleting the options.headless = True and the driver is looking for that option.似乎您正在删除options.headless = True并且驱动程序正在寻找该选项。 Set the headless option to options.headless = False将无头选项设置为options.headless = False

Or write something where headless becomes an optional argument like:或者写一些无头成为可选参数的东西,例如:

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")

UPDATE: I suggested using SeleniumBase as it provides the groundwork for you regarding test suite setup.更新:我建议使用 SeleniumBase,因为它为您提供了有关测试套件设置的基础。

However, it's worth noting that using the following code I was able to toggle between headless and using the browser for tests.但是,值得注意的是,使用以下代码,我能够在无头和使用浏览器进行测试之间切换。 I used homebrew for my local geckodriver requirements on Mac OS 10.15.6 and Python 3.8我在 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