如何在 OOP 结构下的 Python Selenium 中添加驱动程序选项

How to add driver options in Python Selenium under OOP structure

我执行了一个 run.py 文件:

from tasks.tasks import Task

with Task() as bot:
    bot.landing_page()

这是 Task.py 文件:

from selenium import webdriver

import os


class Task(webdriver.Chrome):

    def __init__(self,
                 driver_path=r";C:\Selenium\drivers"):
        self.driver_path = driver_path
        os.environ['PATH'] += self.driver_path

        super(Task, self).__init__()
        self.implicitly_wait(10)
        self.maximize_window()

    def landing_page(self):
        self.get('https://sampleurl.com')

我想添加以下代码,但不确定在何处以及如何添加:

options = Options()
options.add_argument('--incognito')
options.add_argument('--auto-open-devtools-for-tabs')
options.add_experimental_option('excludeSwitches', ['enable-logging'])
    
driver = webdriver.Chrome(options=options)

任何建议将不胜感激

将其作为命名变量添加到超级初始化函数中

from selenium import webdriver

import os


class Task(webdriver.Chrome):

    def __init__(self,
                 driver_path=r";C:\Selenium\drivers"):
        self.driver_path = driver_path
        os.environ['PATH'] += self.driver_path

        options = Options()
        options.add_argument('--incognito')
        options.add_argument('--auto-open-devtools-for-tabs')
        options.add_experimental_option('excludeSwitches', ['enable-logging'])
        super(Task, self).__init__(options=options)
        self.implicitly_wait(10)
        self.maximize_window()

    def landing_page(self):
        self.get('https://sampleurl.com')

更多信息Here