无法创建selenium.webdriver.Chrome的子类

问题描述 投票:0回答:1

我刚刚接触 Python,想向 selenium.webdriver.Chrome 类添加一些额外的行为(我相信)

我想添加新行为

  • 加载jQuery()
  • 等待页面加载() 等等...

但似乎我什至无法创建子类

我的代码

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/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
python selenium oop webdriver
1个回答
0
投票

我找到了,需要做一些改变

  • 从父级

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

  • 功能

    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/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()
© www.soinside.com 2019 - 2024. All rights reserved.