【问题标题】:Python Selenium test does not run when using absolute path to Firefox geckodriver使用 Firefox geckodriver 的绝对路径时,Python Selenium 测试不运行
【发布时间】:2020-12-17 19:13:21
【问题描述】:

我正在尝试在 Linux Ubuntu 环境中的 Python 中运行 Selenium 测试。 Geckodriver 位于我的项目根文件夹中。 我从 PyCharm 命令行运行名为 siteTest.py 的文件:

python3 siteTest.py

但是,我没有看到 Selenium 的任何输出。 在我将其分为 setUp、test 和 tearDown 并添加 self 作为参数之前,该测试已成功。 有什么建议我做错了吗? 提前致谢。

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

【问题讨论】:

    标签: python linux selenium python-unittest geckodriver


    【解决方案1】:

    您的程序近乎完美。您只需将siteTest class 注释为unittest.TestCase。如此有效,您需要重写该行:

    class siteTest:
    

    作为:

    class siteTest(unittest.TestCase):
    

    【讨论】:

    • 非常感谢!
    【解决方案2】:

    您可能需要注释您的设置和拆除方法。

    @classmethod
     def setUp(self)
      .
      .
    
    @classmethod
     def tearDown(self)
      .
      .
    

    在这里,我已将其注释为类方法,因此它只会为该类运行一次。

    【讨论】:

    • 谢谢,我尝试使用你的建议,但仍然没有效果。稍后我会进行实验。
    猜你喜欢
    • 2017-11-01
    • 2021-12-26
    • 2020-09-20
    • 2018-09-27
    • 2014-05-31
    • 2019-03-15
    • 2019-10-26
    • 1970-01-01
    • 2021-06-24
    相关资源
    最近更新 更多