【问题标题】:How to pass options to the Selenium Chrome driver using class in Python?如何在 Python 中使用类将选项传递给 Selenium Chrome 驱动程序?
【发布时间】:2022-12-21 02:16:17
【问题描述】:

我想使用选项以无头模式启动 Chrome 浏览器。 根据文档,我们需要导入选项,例如:

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)

但是,我们如何在如下所述的类中传输选项?

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):
        ...

【问题讨论】:

  • super().__init__(path/to/executable/file, chrome_options=options)

标签: python class google-chrome selenium-webdriver headless-browser


【解决方案1】:

解决方案:

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):
        ...

【讨论】:

    猜你喜欢
    • 2012-09-23
    • 2012-12-02
    • 1970-01-01
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    • 2022-11-21
    相关资源
    最近更新 更多