Selenium Python: What are the errors in my code?
<p>i would like to know why this code opens mozilla twice, and why it doesnÂ´t close it when finishes. Furthermore, i donÂ´t understand 100% why login is a class with a function, and not a function directly.</p>

<pre><code>&gt; import unittest
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class LoginDetails(object): 
    def __init__ (self):
        self.driver = webdriver.Firefox()

    def logindetails(self, username, password):
        driver = self.driver
        driver.maximize_window()
        driver.get("https://miclaro.claro.com.ar/")

        driver.implicitly_wait(30)

        driver.find_element_by_id("_58_login_movil").send_keys(username)

        driver.find_element_by_id("_58_password_movil").send_keys(password)

        driver.find_element_by_id("btn-home-login").click()


        # Login Success

class TestLogin(unittest.TestCase):

    def setUp(self):
        self.ld = LoginDetails()
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)

    def test_sr_Login(self):
        self.ld.logindetails("user", "pass")

    def tearDown(self):
        self.driver.close()

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()
</code></pre>

<p>Thank you very much!</p>

<h1>Firefox opens twice</h1>

<p>In your test <code>self.ld = LoginDetails()</code> runs the <code>__init__</code> function of <code>LoginDetails()</code> which in turn runs <code>webdriver.Firefox()</code> then you issue the same in the next line in the test case. That is why Firefox opens twice. </p>

<h1>Firefox does not close</h1>

<p>For the same reason as above Firefox is not closed. The <code>tearDown</code>  of your test case only closes the instance of <code>webdriver.Firefox()</code> defined in the test case itself not the one opened via the <code>__init__</code> function of the class.</p>

<h1>Why <code>LoginDetails</code> is a class</h1>

<p><code>LoginDetails</code> is a class  in this case to keep <code>webdriver.Firefox()</code> persistent throughout your code. If it would be a function you would open one Firefox session each time you run the function. Unless you specify <code>webdriver.Firefox()</code> outside the function and then pass it to the function.</p>

<h1>Corrected Code</h1>

<p>The following code uses the class functionality:</p>

<pre><code>import unittest
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class LoginDetails(object): 
    def __init__ (self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)

    def logindetails(self, username, password):
        self.driver.maximize_window()
        self.driver.get("https://miclaro.claro.com.ar/")

        self.driver.implicitly_wait(30)

        self.driver.find_element_by_id("_58_login_movil").send_keys(username)

        self.driver.find_element_by_id("_58_password_movil").send_keys(password)

        self.driver.find_element_by_id("btn-home-login").click()

    def __del__(self):
        ''' ADDED based on comment by alecxe '''
        self.driver.close()

class TestLogin(unittest.TestCase):

    def setUp(self):
        self.ld = LoginDetails()

    def test_sr_Login(self):
        self.ld.logindetails("user", "pass")

    def tearDown(self):
        # driver is closed by LoginDetails
        pass

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()
</code></pre>

python selenium selenium-webdriver webdriver