Python selenium child class 打开新浏览器 window - 如何防止这种情况发生?

Python selenium child class opens a new browser window - how to prevent this?

我正在尝试为我的 selenium 浏览器创建 child class。这是我的代码:

class SeleniumDriver:
    """Class that represents a game."""
    def __init__(self):
        # start Tor browser
        self.profile_path = r'C:\Tor Browser2\Browser\TorBrowser\Data\Browser\profile.default'
        self.options = Options()
        self.options.binary_location = r'C:\Tor Browser2\Browser\firefox.exe'
        self.options.set_preference('profile', self.profile_path)
        self.service = Service('C:/Utility/BrowserDrivers/geckodriver/geckodriver.exe')
        self.driver = Firefox(service=self.service, options=self.options)

class Items(SeleniumDriver):
    def __init__(self):
        super().__init__()
    # More methods that do stuff....

当我初始化两个 class 时,child class 启动一个新的浏览器 window。我怎样才能让它附加到 existing/parent 初始化的浏览器?我尝试将“self.driver = Firefox”分离为另一种方法,但随后 child class 抛出:

AttributeError: 'Item' object has no attribute 'driver'

有多种方法可以解决这个问题。

但一般来说,你应该想想你真的需要继承吗?

为什么不委派?

如果您只是想使用由某些 class 启动的驱动程序,则将 driver 作为参数添加到构造函数中,甚至添加 SeleniumDriver 作为参数。

https://erikscode.space/index.php/2020/08/01/delegate-and-decorate-in-python-part-1-the-delegation-pattern/

以驱动程序为参数

class SeleniumDriver:

    driver = None

    def __init__(self):
        ...
        self.driver = Firefox(service=self.service, options=self.options)

class Items:

    driver = None

    # parameterized constructor
    def __init__(self, driver):
        self.driver = driver

...
seleniumDriver = SeleniumDriver()
itemsObj = Items(seleniumDriver.driver)

以 SeleniumDriver 作为参数

class Items:

    seleniumDriver = None

    # parameterized constructor
    def __init__(self, seleniumDriver):
        self.seleniumDriver = seleniumDriver

...

seleniumDriver = SeleniumDriver()
itemsObj = Items(seleniumDriver)

继续使用继承

对于这种情况,我建议从构造函数中移出这一行

self.driver = Firefox(service=self.service, options=self.options)

到单独的 start_driver 方法:

class SeleniumDriver:
    
    def __init__(self):
        self.profile_path = r'C:\Tor Browser2\Browser\TorBrowser\Data\Browser\profile.default'
        self.options = Options()
        self.options.binary_location = r'C:\Tor Browser2\Browser\firefox.exe'
        self.options.set_preference('profile', self.profile_path)
        self.service = Service('C:/Utility/BrowserDrivers/geckodriver/geckodriver.exe')

    def start_driver(self):
        self.driver = Firefox(service=self.service, options=self.options)


class Items(SeleniumDriver):
    def __init__(self):
        super().__init__()

    def with_driver(self, driver):
        self.driver = driver

...
seleniumDriver = SeleniumDriver()
seleniumDriver.start_driver()

itemsObj = Items()
itemsObj.with_driver(seleniumDriver.driver)

或将驱动程序参数添加到构造函数:

class Items(SeleniumDriver):
    def __init__(self, driver):
        super().__init__()
        self.driver = driver

...
seleniumDriver = SeleniumDriver()
seleniumDriver.start_driver()
itemsObj = Items(seleniumDriver.driver) #make sure start_driver() already invoked on seleniumDriver