Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

selenium - Python + WebDriver -- No browser launched while using unittest module

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

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

Thanks a lot for any help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

While working through Python's unittest module with Selenium you have to consider a few facts as follows :

  • While you pass the Key executable_path provide the Value through single quotes along with the raw r 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 valid url e.g. http://www.python.org
  • While you invoke the quit() method within def tearDown(self): invoke the method through the WebDriver instance as self.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: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()
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...