How to avoid getting the object `` NoneType 'does not have the attribute' path '' on selenium quit ()?

When running Selenium Webdriver, the Python script receives the 'NoneType' object has no attribute 'path' after executing self.driver.quit(). . The Enclosing self.driver.quit() application in try/except does not help, namely:

 $ cat demo_NoneType_attribute_error.py # -*- coding: utf-8 -*- from selenium import webdriver import unittest class TestPass(unittest.TestCase): def setUp(self): self.driver = webdriver.Firefox() def test_pass(self): pass def tearDown(self): print("doing: self.driver.quit()") try: self.driver.quit() except AttributeError: pass if __name__ == "__main__": unittest.main() $ python demo_NoneType_attribute_error.py doing: self.driver.quit() 'NoneType' object has no attribute 'path' . ---------------------------------------------------------------------- Ran 1 test in 19.807s OK $ 

Does anyone know how to avoid the message 'NoneType' object has no attribute 'path' ?

Note:
Since this question was already reported by the beginning of November (see URL below), it should have had a patch by now, but updating selenium to the last of pip did not fix it.

Environment: Selenium 3.0.2; Python 2.7 Cygwin 32 bit on Windows 7.

+5
source share
1 answer

selenium 3.0 seems to be a bug

Update the definition of the quit() method in webdriver.py of firefox as follows (relative path: ..\Python27\Lib\site-packages\selenium\webdriver\firefox\webdriver.py ):

change the following line in the quit() method:

 shutil.rmtree(self.profile.path) #which gives Nonetype has no attribute path if self.profile.tempfolder is not None: shutil.rmtree(self.profile.tempfolder) 

to

 if self.profile is not None: shutil.rmtree(self.profile.path) # if self.profile is not None, then only rmtree method is called for path. if self.profile.tempfolder is not None: shutil.rmtree(self.profile.tempfolder) # if tempfolder is not None, then only rmtree is called for tempfolder. 

Note : wherever self.profile used, do the same. those. move the code to the state indicated above.


In selenium 3.0 , profile and binary moved to firefox_options instead of their separate existence as firefox_profile and firefox_binary respectively in Selenium 2.0 .

you can check this in webdriver.py (of firefox ) in the __init__ method.

the corresponding code in the __init__ method:

 if firefox_options is None: firefox_options = Options() print dir(firefox_options) # you can refer binary and profile as part of firefox_options object. 

Note. It has been observed that firefox_options.profile still sets to None , which may be a problem to fix in selenium 3.0

+4
source

Source: https://habr.com/ru/post/1260883/


All Articles