Home > Net >  How to add driver options in Python Selenium under OOP structure
How to add driver options in Python Selenium under OOP structure

Time:12-06

I have one run.py file that I execute:

from tasks.tasks import Task

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

And this is the Task.py file:

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')

I would like to add the following code but not particularly sure where and how:

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)

Any suggestion would be highly appreciated

CodePudding user response:

add it to the super init function as a named var

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')

more info Here

  • Related