Python Selenium-脚本在关闭浏览器之前会打开一个额外的窗口

时间:2018-09-20 21:19:03

标签: python-3.x selenium

以下脚本登录到站点,然后关闭浏览器窗口。一切正常,但我的脚本似乎有问题。在关闭浏览器之前(调用close方法),该脚本似乎打开了一个单独的浏览器窗口,然后关闭了浏览器。

问题是浏览器在Chrome中可以正常关闭,但是在使用Firefox和IE驱动程序的情况下,浏览器窗口仍处于打开状态。

我尝试了其他帮助问题,但没有找到答案。

    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

1 个答案:

答案 0 :(得分:2)

问题在这里:

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

您正在创建两个单独的浏览器实例。要使用相同的Browser实例,请按以下方式实例化:

browser = Browser()
browser.login()
browser.close()