简体   繁体   中英

Selenium on Python with Geckdriver crashes when not in headless mode

I'm trying to use Selenium on a jupyter notebook with geckodriver.

Although on headless mode it seems to work fine, I need to see the browser window since I'm learning...

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

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

The only clue I can find its in the geckodriver.log where I can read:

1596560987355 mozrunner::runner INFO Running command: "/bin/firefox" "-marionette" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofilejjh45q" Error: no DISPLAY environment variable specified

I've checked that that variable exists and it's like DISPLAY:0 . I've also tried to change the value, to set it to my ip and port....

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.

--> Update: I was working Selenium on Jupiter Hub. Neither works on jupyter tree or PyCharm or VSC, but..... works perfect from a script on the terminal... life has mysteries.... xDD

It seems like you're deleting the options.headless = True and the driver is looking for that option. Set the headless option to 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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM