Python + WebDriver-使用unittest模块时未启动浏览器 [英] Python + WebDriver -- No browser launched while using unittest module
本文介绍了Python + WebDriver-使用unittest模块时未启动浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您能帮我下一个吗? 我发现了问题,无法解决. 当我使用下一个代码时,浏览器已启动并且测试已通过:
Could you please help me with the next. I found out the issue and could not resolve it. When I am using next code, the browser has started and the test has passed:
import unittest
from selenium import webdriver
driver = webdriver.Chrome('D:\chromedriver\chromedriver.exe')
driver.get("site URL")
与类和方法相同,但返回消息:进程已完成,退出代码为0":
BUT same with class and methods return message: "Process finished with exit code 0":
import unittest
from selenium import webdriver
class GlossaryPage(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome(executable_path='D:\chromedriver\chromedriver.exe')
self.driver.maximize_window()
self.driver.implicitly_wait(10)
def NoLorem(self):
driver = self.driver
driver.get("site URL")
def tearDown(self):
unittest.quit()
如何使用第二种情况(带有方法和类)打开浏览器?
How can I get the browser opened using 2nd case (with methods and class)?
非常感谢您的帮助.
推荐答案
在通过 Selenium 使用Python的 unittest 模块时,您必须考虑以下几个事实:
While working through Python's unittest module with Selenium you have to consider a few facts as follows :
- 在传递 Key
executable_path
时,请通过单引号和原始的r
开关提供 Value . - 在定义
@Tests
名称时,以 test 开头的测试,例如 def test_NoLorem(自己): - 在调用
get()
时,请确保您传递的是有效的url
,例如 http://www.python.org - 在
def tearDown(self):
中调用quit()
方法的同时,通过 WebDriver 实例将其作为self.driver.quit()
调用. - 如果您使用的是 unittest 模块,则必须通过如果__name__ =="__main__"来调用
Tests
: -
这是您自己的代码,需要进行一些小的修改:
- While you pass the Key
executable_path
provide the Value through single quotes along with the rawr
switch. - While you define the
@Tests
name the tests starting with test e.g. def test_NoLorem(self): - While you invoke
get()
ensure you are passing a validurl
e.g. http://www.python.org - While you invoke the
quit()
method withindef tearDown(self):
invoke the method through the WebDriver instance asself.driver.quit()
. - If you are using unittest module you have to call the
Tests
through if __name__ == "__main__": Here is your own code with the required minor modifications :
import unittest
from selenium import webdriver
class GlossaryPage(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome(executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
self.driver.maximize_window()
self.driver.implicitly_wait(10)
def test_NoLorem(self):
driver = self.driver
driver.get("http://www.python.org")
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()
这篇关于Python + WebDriver-使用unittest模块时未启动浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文