你能帮我做下一个吗?我发现了问题,但无法解决.当我使用下一个代码时,浏览器已经启动并且测试通过了:
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:chromedriverchromedriver.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:chromedriverchromedriver.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 :
executable_path
时,通过单引号和原始 r
开关提供 Value.@Tests
时,测试以 test 开头的名称,例如def test_NoLorem(self):get()
时,请确保您传递的是有效的 url
,例如http://www.python.orgdef tearDown(self) 中调用 quit()
方法时:
通过 WebDriver 实例作为 调用该方法self.driver.quit()
.Tests
这是您自己的代码,并进行了必要的小修改:
executable_path
provide the Value through single quotes along with the raw r
switch.@Tests
name the tests starting with test e.g. def test_NoLorem(self):get()
ensure you are passing a valid url
e.g. http://www.python.orgquit()
method within def tearDown(self):
invoke the method through the WebDriver instance as self.driver.quit()
.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:UtilityBrowserDriverschromedriver.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 模块时没有启动浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!