Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid getting `'NoneType' object has no attribute 'path'` on selenium quit()?

When running Selenium Webdriver Python script, one gets a 'NoneType' object has no attribute 'path' after executing self.driver.quit().
Enclosing self.driver.quit() 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 have an idea how to avoid the 'NoneType' object has no attribute 'path' message?

Note:
Since this issue was already reported by the beginning of November (see URLs below), it should have had a patch by now - but upgrading selenium to latest from pip did not eliminate it.

  • Exception AttributeError: "'NoneType' object has no attribute 'path'" in
  • Exception AttributeError: "'NoneType' object has no attribute 'path'" in

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

like image 515
boardrider Avatar asked Dec 06 '16 15:12

boardrider


People also ask

How can you avoid NoneType object has no attribute?

The Python "AttributeError: 'NoneType' object has no attribute 'get'" occurs when we try to call the get() method on a None value, e.g. assignment from function that doesn't return anything. To solve the error, make sure to only call get() on dict objects.

How do you press a button in Python using selenium?

We can click a button with Selenium webdriver in Python using the click method. First, we have to identify the button to be clicked with the help of any locators like id, name, class, xpath, tagname or css. Then we have to apply the click method on it. A button in html code is represented by button tagname.


1 Answers

It seems a bug in selenium 3.0 version

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

change the following line in 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 is used, do the same. i.e., move the code to if condition as mentioned 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 verify this in webdriver.py (of firefox) in __init__ method.

relevant code in __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: Observed that firefox_options.profile still giving None, which might be an issue to be fixed in selenium 3.0

like image 170
Naveen Kumar R B Avatar answered Oct 13 '22 12:10

Naveen Kumar R B