1

我正在将我的应用程序从程序代码重构为 OOP。我正在尝试做这个驱动程序类。

更新:这适用于 Windows,但不适用于 Mac。

# IMPORTS
from sys import platform
import os
from os import system

from selenium import webdriver
from selenium.webdriver import Firefox, FirefoxOptions
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
# import Action chains 
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import Select
from selenium.webdriver.firefox.service import Service

class Driver():
    def __init__(self):
        #set executable path to driver
        self.dirname = os.path.dirname(__file__)
        if platform == "win32":
            self.executable_path = os.path.join(self.dirname, 'geckodriver.exe') #must save the gecko file to same directory where python is. path to geckodriver (firefox drive/motor) on your machine
            print("Gecko (Firefox) filepath is: ", self.executable_path)
        if platform == "darwin":
            self.executable_path = os.path.join(self.dirname, 'geckodriver') #must save the gecko file to same directory where python is. path to geckodriver (firefox drive/motor) on your machine
            print("Gecko (Firefox) filepath is: ", self.executable_path)
            
        self.service = Service(self.executable_path) 
            
        self.opts = FirefoxOptions()
        #self.opts.add_argument(f"--width={int(screen_width/4)}")
        #self.opts.add_argument(f"--height={int(screen_height/2)}")

        self.driver = Firefox(service=self.service, options=self.opts)
        
        self.driver.set_window_position(-10, 0)
        
        self.driver.get("https://google.com/")
    
Driver()

这会给我以下错误:

Traceback (most recent call last):
  File "/driverClass.py", line 72, in <module>
    Driver()
  File "/driverClass.py", line 66, in __init__
    self.driver = Firefox(service=self.service, options=self.opts)
TypeError: __init__() got an unexpected keyword argument 'service'

为什么是这样?我正在将我的代码重构为 OOP。该代码以前在使用过程代码时有效。

这是来自工作代码:

# driver configs
service = Service(executable_path) #pass in path to geckodriver
opts = FirefoxOptions()
#opts.add_argument(f"--width={int(screen_width/4)}")
#opts.add_argument(f"--height={int(screen_height/2)}")
driver = Firefox(service=service, options=opts)

driver.set_window_position(-10, 0)
#driver.set_window_size(int(screen_width/4), int(screen_height))

driver.get("https://google.com/")
4

3 回答 3

2

此错误消息...

TypeError: __init__() got an unexpected keyword argument 'service'

...暗示这service是一个意想不到的关键字参数。

可能的原因是您仍在使用Selenium v​​3.x并且 不支持关键字参数。 service


解决方案

Selenium 4.0 Beta 1 开始

弃用驱动程序实例化中除Options和参数之外的所有参数。Service(#9125,#9128)

所以你需要升级到Selenium 4.x


参考

您可以在以下位置找到一些相关的详细讨论:

于 2022-02-04T23:10:28.287 回答
1
self.driver = Firefox(service=self.service, options=self.opts)

Firefox webdriver 不接受服务关键字参数: https ://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.firefox.webdriver

查看旧的工作版本会很有帮助。

于 2022-02-04T22:59:28.457 回答
0

正如您在调用时所看到的

Firefox(service=self.service, options=self.opts)

它引发是因为 Firefox 类不接受 service kwarg

如果您尝试设置 Firefox 网络驱动程序,您会看到类似的问题

于 2022-02-04T23:02:08.830 回答