pythonseleniumoopwebdriver

Unable to Create a Child Class of selenium.webdriver.Chrome


I just got into Python and want to add some additional behaviors to the selenium.webdriver.Chrome class ( I believe )

I want to add new behaviors

But it seems like I can't even create a child class

My Code

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

class Chrome(webdriver.Chrome):
    def __init__(self):
        self.options = Options()
        self.driverPath = "drivers/chromedriver"
        self.jQueryJs = "Js/jquery-3.6.0.min.js"
        self.hasjQuery = False

        super().__init__(self, self.driverPath, self.options)

    def loadjQuery(self):
        with open(self.jQueryJs, errors='ignore') as jQueryJsSource:
            self.execute_script(jQueryJsSource.read())
            self.hasjQuery = True

    def pageWait(self):
            ready_state = self.execute_script('return document.readyState')
            times = 0
            while ready_state != "complete":
                time.sleep(0.5)
                times += 1
                if times > 20:
                    break
            time.sleep(2)
            return self

chrome = Chrome()
chrome.loadjQuery()
chrome.get("http://google.com")
chrome.waitForPageLoad()

This is the Output Error

Traceback (most recent call last):
  File "/home/user/.local/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3437, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-c077212233b1>", line 30, in <module>
    chrome = Chrome()
  File "<ipython-input-2-c077212233b1>", line 12, in __init__
    super().__init__(self, self.driverPath, self.options)
  File "/home/user/.local/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
    self.service.start()
  File "/home/user/.local/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 71, in start
    cmd.extend(self.command_line_args())
  File "/home/user/.local/lib/python3.8/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 str

Solution

  • I found it, there ware few changes to be made

    Working Code

    from selenium.webdriver.chrome.options import Options
    from selenium import webdriver
    import time
    
    class Chrome(webdriver.Chrome):
        def __init__(self):
            self.options = Options()
            self.driverPath = "drivers/chromedriver"
            self.jQueryJs = "/home/user/Documents/python-projects/lgbt/LGBT-AdvancedWebScrape/Resourse/Js/jquery-3.6.0.min.js"
            self.hasjQuery = False
    
            super().__init__( self.driverPath, options = self.options)
    
        def loadjQuery(self):
            with open(self.jQueryJs, errors='ignore') as jQueryJsSource:
                self.execute_script(jQueryJsSource.read())
                self.hasjQuery = True
    
        def pageWait(self):
                ready_state = self.execute_script('return document.readyState')
                times = 0
                while ready_state != "complete":
                    time.sleep(0.5)
                    times += 1
                    if times > 20:
                        break
                time.sleep(2)
                return self
    
    chrome = Chrome()
    chrome.loadjQuery()
    chrome.get("http://google.com")
    chrome.pageWait()