Skip to main content
Corrected question heading and body, tag edits
Source Link
undetected Selenium
  • 194.5k
  • 44
  • 304
  • 387

Selenium TypeError: __init__() got an unexpected keyword argument 'service'. Why in this case?

# 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_pathservice = 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_pathservice, 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_path''service' 

Selenium TypeError: __init__() got an unexpected keyword argument 'service'. Why in this case?

# 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_path = 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_path, 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_path' 

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

# 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' 
added 15 characters in body
Source Link
AppCreator
  • 241
  • 1
  • 3
  • 16

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.serviceservice_path = 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.serviceservice_path, 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''service_path' 
# 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' 

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_path = 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_path, 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_path' 
added 306 characters in body
Source Link
AppCreator
  • 241
  • 1
  • 3
  • 16
# IMPORTS from sys import platform import os from os import system if platform == "win32": import win32gui # import module to set focus to specified window so that hotkeys work on that. import win32con  import win32api 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() 

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/") 
# IMPORTS from sys import platform import os from os import system if platform == "win32": import win32gui # import module to set focus to specified window so that hotkeys work on that. import win32con  import win32api 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() 

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

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

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/") 
full stacktrace
Source Link
AppCreator
  • 241
  • 1
  • 3
  • 16
Loading
Source Link
AppCreator
  • 241
  • 1
  • 3
  • 16
Loading