无法创建 Child Class of selenium.webdriver.Chrome

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

我刚进入 Python 并想向 selenium.webdriver.Chrome class 添加一些额外的行为(我相信)

我想添加新行为

但我好像连创建一个childclass

都做不到

我的代码

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()

这是输出错误

Traceback (most recent call last):
  File "/home/yatnam/.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/yatnam/.local/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
    self.service.start()
  File "/home/yatnam/.local/lib/python3.8/site-packages/selenium/webdriver/common/service.py", line 71, in start
    cmd.extend(self.command_line_args())
  File "/home/yatnam/.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

我找到了,需要做的改动很少

  • 从父 __init__ 调用中删除了 self 以正确匹配参数顺序(这导致了我在问题中发布的原始错误)

  • 函数waitForPageLoad()原来的名字是pageWait()

工作代码

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/yatnam/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()