简体   繁体   English

Python Selenium 当使用 Firefox geckodriver 的绝对路径时,测试不运行

[英]Python Selenium test does not run when using absolute path to Firefox geckodriver

I am trying to run Selenium test in Python on Linux Ubuntu environment.我正在尝试在 Linux Ubuntu 环境中的 Python 中运行 Selenium 测试。 Geckodriver is located in my project root folder. Geckodriver 位于我的项目根文件夹中。 I run the file named siteTest.py from PyCharm command line:我从 PyCharm 命令行运行名为 siteTest.py 的文件:

python3 siteTest.py python3 站点测试.py

However, I do not see any output from Selenium. The test worked before I divided it into setUp, test and tearDown and added self as a parameter.但是,我没有从 Selenium 中看到任何 output。在我将其分为 setUp、test 和 tearDown 并添加self作为参数之前,测试有效。 Any suggestions what I am doing wrong?有什么建议我做错了吗? Thanks in advance.提前致谢。

import os
import unittest
 
from selenium import webdriver
 
 
class siteTest:
    def setUp(self):
        ROOT_DIR = os.path.abspath(os.curdir)
        self.driver = webdriver.Firefox(executable_path=ROOT_DIR + '/geckodriver')
 
    def test(self):
        driver = self.driver
        driver.get('https://google.com/')
 
    def tearDown(self):
        self.driver.quit()
 
 
if __name__ == "__main__":
    unittest.main()

Your program was near perfect.你的程序近乎完美。 You just need to annotate the siteTest class as unittest.TestCase .您只需siteTest class注释为unittest.TestCase So effectively, you need to rewrite the line:如此有效,您需要重写该行:

class siteTest:

as:作为:

class siteTest(unittest.TestCase):

You probably need to annotate your set up and tear down methods.您可能需要注释您的设置和拆卸方法。

@classmethod
 def setUp(self)
  .
  .

@classmethod
 def tearDown(self)
  .
  .

Here, I have annotated as class method so it will run only once for the class.在这里,我已经注释为 class 方法,因此它只会为 class 运行一次。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 'geckodriver' 可执行文件需要在 PATH 中使用 GeckoDriver 和 Firefox 到 Selenium - 'geckodriver' executable needs to be in PATH using GeckoDriver and Firefox through Selenium Python Selenium 和 FireFox \\ geckodriver - Python Selenium and FireFox \ geckodriver 为什么 Selenium 和 geckodriver 在使用 airflow 测试运行时可以工作,但在 DAG 运行中运行时会引发错误? - Why does Selenium and geckodriver work when run using airflow test but raises an error when running inside a DAG run? Selenium 使用 Python - Geckodriver 可执行文件需要在 PATH 中 - Selenium using Python - Geckodriver executable needs to be in PATH OSError:[Errno 8] Exec格式错误:'geckodriver'试图在python中使用selenium打开firefox - OSError: [Errno 8] Exec format error: 'geckodriver' when trying to open firefox using selenium in python 在Firefox GeckoDriver中使用Python Selenium WebDriver时出现无效的安全证书错误 - Invalid Security Cert error when using Python Selenium WebDriver with Firefox GeckoDriver Python Selenium 路径中需要壁虎驱动程序 - Python Selenium geckodriver required in path 使用GeckoDriver和Python的Selenium firefox webdriver未知错误 - Selenium firefox webdriver unknown error using GeckoDriver and Python selenium.common.exceptions.WebDriverException:消息:'firefox' 可执行文件需要在 GeckoDriver Firefox Selenium 和 Python 的 PATH 中 - selenium.common.exceptions.WebDriverException: Message: 'firefox' executable needs to be in PATH with GeckoDriver Firefox Selenium and Python Python Selenium Geckodriver 应该在 PATH 中 - Python Selenium Geckodriver should be in PATH
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM