Home > Blockchain >  How to add webdriver extension arguments on super() in python selenium?
How to add webdriver extension arguments on super() in python selenium?

Time:10-08

I tried to add chrome extension when open chrome webdriver in python.

OK.py

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

extension_path = r'C:/Users/path/to/extension'

chrome_options = Options()
chrome_options.add_argument('load-extension='   extension_path)
chrome_options.add_argument("--disable-notifications")
chrome_options.add_argument("--disable-infobars")

browser = webdriver.Chrome(executable_path=r'C:/Users/path/to/chromedriver.exe', options = chrome_options)

The codes runs fine and the extension was presented. However, when I split into 2 files and use classes.

variables.py

from selenium.webdriver.chrome.options import Options

driver_path = r'C:/Users/path/to/chromedriver.exe'
extension_path = r'C:/Users/path/to/extension'

chrome_options = Options()
chrome_options.add_argument('load-extension='   extension_path)
chrome_options.add_argument("--disable-notifications")
chrome_options.add_argument("--disable-infobars")

broken.py

import variables as vb
from selenium import webdriver

class nokair(webdriver.Chrome):
    def __init__(self, executable_path = vb.driver_path, options = vb.chrome_options):
        self.driver_path = executable_path
        self.options = options
        super().__init__()

init = nokair()

When launched, the webdriver was executed without any extension. I've tried other workaround

import variables as vb
from selenium import webdriver

class nokair(webdriver.Chrome):
    def __init__(self, executable_path = vb.driver_path, options = vb.chrome_options):
        self.driver_path = executable_path
        self.options = options
        super().__init__(executable_path, options)

init = nokair()

This threw an error. What did I do wrong?

Traceback (most recent call last):
  File "c:\Users\Kittinun\Desktop\VS workspace\Playground\Flight\nokair.py", line 93, in <module>
    nok = nokair()
  File "c:\Users\Kittinun\Desktop\VS workspace\Playground\Flight\nokair.py", line 14, in __init__
    super(nokair, self).__init__(executable_path, options)
  File "C:\ProgramData\Miniconda3\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 73, in __init__
    self.service.start()
  File "C:\ProgramData\Miniconda3\lib\site-packages\selenium\webdriver\common\service.py", line 71, in start
    cmd.extend(self.command_line_args())
  File "C:\ProgramData\Miniconda3\lib\site-packages\selenium\webdriver\chrome\service.py", line 45, in command_line_args
    return ["--port=%d" % self.port]   self.service_args
TypeError: %d format: a number is required, not Options

CodePudding user response:

broken.py does not work because there is not options field in webdriver.Chrome, hence it is not used by that driver.

The latter case does not work because you are not using named parameters which forces Python to assign them sequentially. The second parameter of the constructor is port. But you pass Options to it.

Workable code would be

import variables as vb
from selenium import webdriver

class nokair(webdriver.Chrome):
    def __init__(self):
        super().__init__(executable_path=vb.driver_path, options=vb.chrome_options)

init = nokair()

In my example I left nokair constructor without any parameter to simplify the things and not mess up parameter names.

  • Related