简体   繁体   English

OOP结构下Python Selenium如何添加驱动选项

[英]How to add driver options in Python Selenium under OOP structure

I have one run.py file that I execute:我有一个执行的run.py文件:

from tasks.tasks import Task

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

And this is the Task.py file:这是Task.py文件:

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任何建议将不胜感激

add it to the super init function as a named var将其作为命名变量添加到超级初始化 function

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更多信息在这里

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM