Python 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
jarcobi889

问题在这里:

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Python Selenium保持浏览器打开

我们可以在python selenium webdriver中缩放浏览器窗口吗?

Selenium:在不关闭浏览器的情况下退出Python脚本

Selenium Python最小化浏览器窗口

Python Selenium:使用已经打开并使用登录凭证登录的浏览器

使用Selenium Python和Firefox重新打开同一浏览器窗口

Python和Selenium-关闭所有选项卡而不关闭浏览器

Python Selenium保持浏览器打开以接收新数据

如何在Python中使用Selenium在由不同WebDriver打开的不同Chrome浏览器窗口之间切换?

Python Selenium While Loop只会首先打开浏览器

Python Selenium:如何使浏览器窗口不打开

使用Selenium Python在同一浏览器中打开多个URL

Python:Selenium Webdriver Chrome打开浏览器,但未打开所选站点

无法在Python Selenium中重新打开浏览器

Python / Selenium:在几个下拉菜单中选择所有相同的类,选择一个值,刷新浏览器并重新启动脚本

如何在testng for selenium中使浏览器的打开(在一个浏览器中全部测试)静态

Selenium驱动程序无法打开浏览器窗口(Python)

Python / Celery / Selenium连续任务(避免重新打开浏览器)

每次启动CMD都会打开一个新的文件浏览器窗口

在 Selenium Python 中关闭浏览器弹出窗口

如何在脚本执行时打开浏览器的 Python 和 Selenium 脚本设置 crontab

浏览器窗口打开 URL 然后在 Selenium Python 中突然关闭

当我在 vscode 中运行 python 时,它会在我的浏览器中打开一个选项卡

避免在 Selenium Python 中为每个实例打开一个新浏览器

如何使用 Selenium 和 python 在浏览器的第二个窗口上单击 A 按钮

Python、selenium - 如何在不显示浏览器窗口的情况下运行脚本

Python selenium 如何保持浏览器打开?

我需要使用 Python+Selenium 打开一个新选项卡并将 URL 粘贴到浏览器的 Url 行。由于某种原因不能使用 Keys

浏览器选项卡在 selenium python 中关闭