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>

<p>This is because you <em>instantiate webdriver twice</em> - once inside the <code>TestCase</code> and once inside the <code>LoginDetails</code> class.</p>

<h1>Why the other answer is not entirely correct</h1>

<p>The WebDriver should not be controlled by the <code>LoginDetails</code> class in this case. <code>LoginDetails</code> class is very close to a <a href="http://selenium-python.readthedocs.io/page-objects.html" rel="nofollow">Page Object</a> notation representation and, hence, should be given the driver "from outside". Plus, opening browser in one class and closing it in the other is making the code close to <a href="https://en.wikipedia.org/wiki/Spaghetti_code" rel="nofollow">"Spaghetti"</a>.</p>

<h1>Better solution</h1>

<p>Control the <code>webdriver</code> from the <code>TestCase</code> class and "share" with the <code>LoginDetails</code>:</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, driver):
        self.driver = driver

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


class TestLogin(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.ld = LoginDetails(self.driver)

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

    def tearDown(self):
        self.driver.close()
</code></pre>

python selenium selenium-webdriver webdriver