5

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.

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

boardrider
  • 5,882
  • 7
  • 49
  • 86
  • 1
    this was fixed in selenium 3.0.2. you *must* be on an older version. https://github.com/SeleniumHQ/selenium/commit/9157c7071f9900c2608f5ca40ae4f518ed373b96 – Corey Goldberg Dec 12 '16 at 18:51

1 Answers1

6

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

Naveen Kumar R B
  • 6,248
  • 5
  • 32
  • 65
  • 1
    Thanks @Naveen, worked as advertised: if you have the time (and inclination), why not write a patch - implementing your suggestion - and get gloria mundi? – boardrider Dec 07 '16 at 11:31
  • @boardrider, glad it worked for you. I need to know the selenium webdriver in its totality in order to make changes (or fix things). the answer is just my perspective and I don't know the real reason behind those changes, so can't comment on that. – Naveen Kumar R B Dec 07 '16 at 12:00
  • Let's hope the Selenium guys monitor SO, so they'd get a nudge to create a patch themselves... – boardrider Dec 07 '16 at 19:45
  • 1
    @boardrider you could at least file an issue with the Selenium team referring to this thread. – shadowtalker Dec 12 '16 at 00:27
  • wasn't this already fixed with https://github.com/SeleniumHQ/selenium/commit/9157c7071f9900c2608f5ca40ae4f518ed373b96 ? – Corey Goldberg Dec 12 '16 at 16:32
  • @CoreyGoldberg, just checked. It is fixed. updated selenium using `pip install -U selenium`, which upgraded selenium from `3.0.1` to `3.0.2`, which has `if condition` to check `profile` is not None. – Naveen Kumar R B Dec 12 '16 at 16:52
  • @boardrider, try by upgrading selenium version using `pip install -U selenium` and confirm as you are already using `3.0.2` version. – Naveen Kumar R B Dec 13 '16 at 06:25
  • `pip list | grep -i selen` gives: *selenium (3.0.2)* – boardrider Dec 13 '16 at 19:52
  • @boardrider, try as suggested in the issue logged by you here https://github.com/SeleniumHQ/selenium/issues/3239. suggest running the command `pip install -U selenium` again (I knew you are already using `3.0.2` version but that may not be the latest). – Naveen Kumar R B Dec 14 '16 at 05:50
  • 1
    I implement your answer's suggestions @Naveen, and my code works okay now. So, since I'm already at `3.0.2` I prefer not to `pip`(as that would/may overwrite your changes ("if it ain't broken" and all that). – boardrider Dec 14 '16 at 20:37