简体   繁体   中英

How to pass options to the Selenium Chrome driver using class in Python?

I would like to initiate Chrome browser in headless mode using options. Based on the documentation, we need to import Options such as:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('--headless')
driver = webdriver.Chrome(path/to/executable/file, chrome_options=options)

However, how can we transmit the Options in a class such as described below?

class Browser(webdriver.Chrome):
    def __init__(self):
        self.driver_path = r"path/to/executable/file"
        os.environ['PATH'] += os.pathsep + self.driver_path
        super(Browser, self).__init__()

    def some_function(self):
        ...

Solution:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument('--headless')

class Browser(webdriver.Chrome):
    def __init__(self, driver_path=r"path/to/executable/file"):
        self.driver_path = driver_path
        os.environ['PATH'] += self.driver_path
        super(Browser, self).__init__(options=chrome_options)

    def some_function(self):
        ...

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