使用unittest无法使用python运行selenium

时间:2017-08-15 05:48:59

标签: python python-2.7 selenium

使用以下配置:

  

macOS Sierra 10.12.4,Python 2.7,Selenium v​​ersion 3.5

我正在尝试使用selenium webdriver和unittest模块在Pycharm中编写脚本。我的目标是去一个站点找到元素(按钮)并单击按钮。我尝试使用Firefox驱动程序和Chrome驱动程序。我也尝试在没有IDE但没有运气的情况下运行它。我一直在寻找,但无法上班。

这是我的代码:

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys


class HomeWorkTeam(unittest.TestCase):


    def setUp(self):
        self.driver = webdriver.Chrome(executable_path=r'/Users/<user>/HomeWork/chromedriver')


    def test_team_search(self):
        driver = self.driver
        driver.get("http://www.python.org/index.html")
        teamelem = driver.find_element_by_id("submit")
        teamelem.send_keys(Keys.ENTER)
        assert "No results found" not in driver.page_source


    def tearDown(self):
        self.driver.quit()

    if __name__ == '__main__':
        unittest.main()

当我运行它时,这是输出但是浏览器没有启动。

  

在0.000秒内进行0次测试

     

     

处理完成,退出代码为0

2 个答案:

答案 0 :(得分:0)

我假设您使用下面的

运行它
python -m unittest

如果您使用此测试的文件是myunit,那么您需要使用以下内容来运行它

$ python -m unittest -v myunit

test_team_search (myunit.HomeWorkTeam) ... ok

----------------------------------------------------------------------
Ran 1 test in 6.455s

OK

PS:有关详细信息,请参阅https://docs.python.org/devguide/runtests.html

答案 1 :(得分:0)

setx@classmethod方法添加setUp。另外,删除if语句的缩进。缩进与类方法相同。

teardown