简体   繁体   中英

Selenium keeps opening new window

I made GUI with PyQT5, and some pushbuttons are connected to codes using Selenium.

The buttons work well, but problem is that Selenium keeps opening new window when I click the button.

If I define variable "driver = webdriver.Edge(~~)" outside of class, Selenium works fine in one window. However, I want to open the browser when I click the buttons, not at the start of the code.

1. btn_1_clicked and btn_2_clicked work in one window, but the problem is that the browser opens as soon as I run the code.

import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
from PyQt5.QtCore import QCoreApplication, Qt
from selenium import webdriver
    
driver = webdriver.Edge('C:/Users/HOME/Desktop/MicrosoftwebDriver.exe')

form_class = uic.loadUiType('C:/Users/HOME/Desktop/test.ui')[0]

class WindowClass(QMainWindow, form_class):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        self.btn_1.clicked.connect(self.btn_1_clicked)
        self.btn_2.clicked.connect(self.btn_2_clicked)

    def btn_1_clicked(self):
        driver.get("https://stackoverflow.com/")

    def btn_2_clicked(self):
        driver.get("https://www.google.com")

if __name__ == "__main__":
    app = QCoreApplication.instance()
    if app is None:
        app = QApplication(sys.argv)
    myWindow = WindowClass()
    myWindow.show()
    app.exec_()

2. Browser opens only when I click the buttons, but the problem is that Selenium keeps opening new window.

import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic
from PyQt5.QtCore import QCoreApplication, Qt
from selenium import webdriver

form_class = uic.loadUiType('C:/Users/HOME/Desktop/test.ui')[0]

class WindowClass(QMainWindow, form_class):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

        self.btn_1.clicked.connect(self.btn_1_clicked)
        self.btn_2.clicked.connect(self.btn_2_clicked)

    def btn_1_clicked(self):
        driver = webdriver.Edge('C:/Users/HOME/Desktop/MicrosoftwebDriver.exe')
        driver.get("https://stackoverflow.com/")

    def btn_2_clicked(self):
        driver = webdriver.Edge('C:/Users/HOME/Desktop/MicrosoftwebDriver.exe')
        driver.get("https://www.google.com")

if __name__ == "__main__":
    app = QCoreApplication.instance()
    if app is None:
        app = QApplication(sys.argv)
    myWindow = WindowClass()
    myWindow.show()
    app.exec_()

Thanks in advance.

You should make the driver an instance attribute, and create it only when required.

Using cached_property the driver is loaded the first time the property is requested and then it will always use the returned driver:

from functools import cached_property

class WindowClass(QWidget):
    # ...
    @cached_property
    def driver(self):
        return webdriver.Edge('C:/Users/HOME/Desktop/MicrosoftwebDriver.exe')

    def btn_1_clicked(self):
        self.driver.get("https://stackoverflow.com/")

    def btn_2_clicked(self):
        self.driver.get("https://www.google.com")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM