2

I'm refactoring my app from procedural code to OOP. I am trying to do this Driver class.

UPDATE: this works in Windows but not in 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() 

This will give me the following error:

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' 

Why is this? I'm refactoring my code to OOP. The code worked before when using procedural code.

This is from the working code:

# 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/") 
1
  • Provide full stacktrace Commented Feb 4, 2022 at 22:56

4 Answers 4

3

This error message...

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

...implies that service is an unexpected keyword argument.

The possible reason, you are still using Selenium v3.x and the keyword argument service wasn't supported.


Solution

Since Selenium 4.0 Beta 1:

Deprecate all but Options and Service arguments in driver instantiation. (#9125,#9128)

So you need to upgrade to Selenium 4.x


References

You can find a couple of relevant detailed discussion in:

Sign up to request clarification or add additional context in comments.

8 Comments

I tried your solution to change the name to ´self.service_path´ but the same error occurs.
Changed the second line as well?
Yes absolutely. self.driver = Firefox(service=self.service_path, options=self.opts)
Are you executing the refactored code on the same machine?
I tested it on windows and it works. But the error occurs when testing on Mac.
|
1

I encountered the same error and fixed this problem by upgrading to Selenium 4.0

I am using Anaconda python.

To upgrade to Selenium 4.0 on Anaconda, run the following command;

$ conda install https://conda.anaconda.org/conda-forge/noarch::selenium 

The following command also works fine.

$ conda install selenium --channel conda-forge/noarch 

At this point of writing, you cannot upgrade Selenium using the normal conda command conda update selenium. Running this command will install selenium v3

enter image description here

You have to specify the installation on the noarch version which supports selenium v4.1

The link below teaches you how to do that.

Specify platform during package install

Comments

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

The Firefox webdriver doesn't take a service keyword argument: https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.firefox.webdriver

It would be helpful to see the older working version.

6 Comments

I added the working version
Are you using the same version of the selenium module in both cases?
What do the import statements look like in the working case?
It is the same import statements in both cases.
I tested it on windows and it works. But the error occurs when testing on Mac.
|
0

As you can see when you call

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

it raises because Firefox class does not accept service kwarg

If you are trying to set up a Firefox web driver you can see similar question

1 Comment

Then why is my older code working that also has service kwarg?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.