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



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

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)

然而,我们如何在如下所述的类中传输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):
...

解决方案:

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

相关内容

  • 没有找到相关文章

最新更新