简体   繁体   中英

Trying to open Firefox via Selenium fails when running from VSCode

I'm trying to open Firefox via Selenium and it fails. It worked fine with Chrome, but when I tried Firefox I can't get it to work. It fails when trying to create the webdriver object, throwing selenium.common.exceptions.WebDriverException: Message: invalid argument: can't kill an exited process

The code I execute is:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time
import json
import os

if __name__ == "__main__":
    driver = webdriver.Firefox(executable_path='./geckodriver')

with the geckodriver file in the same folder as the python file.

After searching online I found it might be because of incompatible versions of geckodriver and Firefox but I have Firefox 72.0.1-64bit and use the geckodriver v0.26.0-linux64.

Edit: I have found that the problem does NOT occur when running normally from terminal, but only when running the python code from the VSCode terminal. Is there any way to fix this issue?

Try running it with {global parameters} and {some functions} commented out.

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
import time
import json
import os

#{global parameters}
#{some functions}

if __name__ == "__main__":
    driver = webdriver.Firefox(executable_path=r'C:\\Path\\To\\geckodriver.exe')

I can execute this file from the command line by cd ing to the location of the file and running python file_name.py assuming the text above is in a file called file_name.py .

Not that attempting to run this from the VSCode terminal was not successful for the asker of this question.

I'm no expert, so I hope you'll tolerate my ignorance of suchAfter a week of struggling with the same error, I found that I needed the following line typed at the command line ($) of the environment I was using:

export DISPLAY=":0"

In my case, this allowed Visual Studio Code 1.46.1 to open an output screen on my console when running Python 3.6.9 with Selenium 3.141.0 and Pytest 5.4.3 modules. Will it work for you? Don't know. It was such a simple thing that worked. Not sure why. Something to do with session display output routing? NOTE: When I want to run the Python program using the Selenium webdriver WITHOUT the session display output coming to my console, I can run the webdriver "headless".

Hope this helps more than hinders...

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