简体   繁体   English

如何在 Python 中使用类将选项传递给 Selenium Chrome 驱动程序?

[英]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.我想使用选项以无头模式启动 Chrome 浏览器。 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):
        ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Python 将选项传递给 Selenium Chrome 驱动程序? - How do I pass options to the Selenium Chrome driver using Python? 如何使用Selenium,Chrome驱动程序和Python关闭新建的标签页 - How to close newly constructed tab using selenium, chrome driver and python 如何通过 Python 使用 Chrome 驱动程序和 Selenium 登录 Applemusic - How To sign in to Applemusic With Python Using Chrome Driver With Selenium 如何在 Z23EEEB4347BDD755BFC6B7EE9A 中使用 selenium firefox 和 chrome 驱动程序翻译 web 页面? - How to translate a web page using selenium firefox and chrome driver in python? 将 selenium chrome 驱动与 python 一起使用时如何避免验证页面? - How to avoid verification page when using selenium chrome driver with python? 如何在 Selenium Python 中更新 firefox 驱动程序选项 - How to update firefox driver options in Selenium Python Selenium和Python的错误以及使用Chrome驱动程序 - Errors with Selenium and Python and Using Chrome Driver Python Selenium Chrome驱动程序 - Python selenium Chrome driver 如何在 Python 中更改 chrome selenium 驱动程序的地理位置? - How to change geolocation of chrome selenium driver in Python? Python如何将Selenium驱动程序传递给函数 - Python how to pass selenium driver into function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM