从VSCode运行时尝试通过Selenium打开Firefox失败

问题描述 投票:1回答:1

我正在尝试通过Selenium打开Firefox,但失败。它可以在Chrome上正常运行,但是当我尝试使用Firefox时,我无法使其正常运行。尝试创建webdriver对象时失败,抛出selenium.common.exceptions.WebDriverException: Message: invalid argument: can't kill an exited process

我执行的代码是:

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

geckodriver文件与python文件位于同一文件夹中。

在线搜索后,我发现可能是由于geckodriver和Firefox版本不兼容,但我使用的是Firefox 72.0.1-64bit,并且使用了geckodriver v0.26.0-linux64。

编辑:我发现从终端正常运行时不会发生此问题,而只有从VSCode终端运行python代码时才会发生。有什么办法可以解决此问题?

python selenium firefox visual-studio-code geckodriver
1个回答
1
投票

尝试以{global parameters}{some functions}注释掉运行它。

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

我可以从命令行通过cd执行到该文件的位置,然后运行python file_name.py并假设上面的文本在名为file_name.py的文件中,然后运行它。

不是这个问题的提问者尝试从VSCode终端运行此命令并不成功。

© www.soinside.com 2019 - 2024. All rights reserved.