2

The following script logs in to a site and then closse the browser window. Everything works fine but there seems to be an issue with my script. Before closing the browser(calling the close method), the script seems to open a separate browser window and then close the browser.

The problem is that the browser closes fine in Chrome but with Firefox and IE drivers, the browser windows are still open.

I tried other help questions but didn't find an answer.

    import sys
    import argparse
    from selenium import webdriver
    import datetime

    parser = argparse.ArgumentParser()
    parser.add_argument('browser', default='chrome', help='Types of browser:chrome, firefox, ie')
    parser.add_argument('username', help='This is the  username')
    parser.add_argument('password', help='This is the  password')
    args = parser.parse_args()

    setup_parameters = sys.argv[1:]


    class Browser(object):

        url = 'https:someurl'
        start_time = datetime.datetime.today()


        def __init__(self):
            self.username = setup_parameters[1]
            self.password = setup_parameters[2]
            if setup_parameters[0] == 'chrome':
                self.browser = webdriver.Chrome('C:\Python37\chromedriver.exe')
                print("Running tests on Chrome browser on %s" % self.start_time)


            elif setup_parameters[0] == 'ie':
                self.browser = webdriver.Ie()
                print("Running tests on Internet Explorer browser on %s" % self.start_time)


            elif setup_parameters[0] == 'firefox':
                self.browser = webdriver.Firefox()
                print("Running tests on Firefox browser on %s" % self.start_time)


            elif setup_parameters[0] == 'None':
                print('No browser type specified.... continuing with the default browser')
                self.browser = webdriver.Chrome()

        def login(self):
            # Method used to log in to the site
            self.browser.get(self.url)
            self.browser.implicitly_wait(10)
            self.browser.maximize_window()
            self.browser.find_element_by_id("Username").send_keys(self.username)
            self.browser.find_element_by_id("Password").send_keys(self.password)
            self.browser.find_element_by_id("btnLogin").click()

        def close(self):
            # Closing the browser window and terminating the test
            self.browser.close()
            print("Test(s) ended on {} at {}".format(setup_parameters[0], datetime.datetime.today()))


    if __name__ == '__main__':
        Browser().login()
        Browser().close()

This is the output when I ran the above script.

    C:\Users\PycharmProjects\Automation>python Web_Login.py chrome  ADMIN password

    [3676:10208:0920/165839.699:ERROR:install_util.cc(629)] Failed to read HKLM\SOFTWARE\Policies\Google\Chrome\MachineLevelUserCloudPolicyEnrollmentToken: The system cannot find the file s
    pecified. (0x2)

    DevTools listening on ws://127.0.0.1:52955/devtools/browser/352b4801-28db-4be5-a54a-904d549738b5

    Running tests on Chrome browser on 2018-09-20 16:58:35.394186

    [3104:15052:0920/165914.271:ERROR:install_util.cc(629)] Failed to read HKLM\SOFTWARE\Policies\Google\Chrome\MachineLevelUserCloudPolicyEnrollmentToken: The system cannot find the file s
    pecified. (0x2)

    DevTools listening on ws://127.0.0.1:53240/devtools/browser/4f2fa155-05d7-4209-82e0-ab7839259912

    Running tests on Chrome browser on 2018-09-20 16:58:35.394186

    Test(s) ended on chrome at 2018-09-20 16:59:16.411363
Dev Bin
  • 93
  • 1
  • 10
  • Does this [discussion](https://stackoverflow.com/questions/52401561/failed-to-read-hklm-software-policies-google-chrome-machinelevelusercloudpolicye/52403734#52403734) helps you? – undetected Selenium Sep 20 '18 at 21:31
  • That discussion doesn't help answer my question but thanks for the suggestion though. – Dev Bin Sep 20 '18 at 21:34
  • You really shouldn't use `.close()` unless you have a need to keep the browser open (as it closes the current tab). Use `.quit()` instead. – Lucas Tierney Sep 21 '18 at 01:57

1 Answers1

2

The problem is here:

Browser().login()
Browser().close()

You are creating two separate Browser instances. To use the same Browser instance, instantiate like this:

browser = Browser()
browser.login()
browser.close()
jarcobi889
  • 815
  • 5
  • 16