Selenium TypeError: __init__() got an unexpected keyword argument #39;service#39;(Selenium类型错误:__init__()获得意外的关键字参数#39;service#39;)
本文介绍了Selenium类型错误:__init__()获得意外的关键字参数';service';的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在将我的应用程序从过程代码重构为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/")
推荐答案
此错误消息.
TypeError: __init__() got an unexpected keyword argument 'service'
.表示service
是意外的关键字参数。
可能的原因,您仍在使用Seleniumv3.x和关键字参数service
不受支持。
解决方案
自Selenium 4.0 Beta 1起:
不推荐使用驱动程序实例化中除Options
和Service
参数之外的所有参数。(#9125,#9128)
因此您需要升级到Selenium 4.x
引用
您可以在以下位置找到几个相关的详细讨论:
- TypeError: init() got an unexpected keyword argument 'service' error using Python Selenium ChromeDriver with company pac file
这篇关于Selenium类型错误:__init__()获得意外的关键字参数';service';的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:Selenium类型错误:__init__()获得意外的关键字参数';service';


基础教程推荐
猜你喜欢
- Python 的 List 是如何实现的? 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01
- 将 YAML 文件转换为 python dict 2022-01-01
- 症状类型错误:无法确定关系的真值 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01