I’m creating an automation program and I have created a browser automation class that handles the automation and opening of the Chrome browser. I have split my code into two files which are main.py (containing all classes and methods) and automate.py (which uses the created classes and methods). Below is my source code for the two files.
I want to run the code so that the Chrome tab to opened automatically with the URL provided.
main.py
import sys
import logging
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
LOGGER = logging.getLogger()
class BrowserAutomation(object):
def __init__(self, browser=None, time_out=1000):
"A method to deal with browser opening"
self.BASE_URL ="https://erita.rita.go.tz/auth"
if not browser:
browser = webdriver.Chrome(
ChromeDriverManager().install(),
options=self.chrome_options,
)
handles = browser.window_handles
for _, handle in enumerate(handles):
if handle != browser.current_window_handle:
browser.switch_to.window(handle)
browser.close()
self.browser = browser
self.wait = WebDriverWait(self.browser, time_out)
self.cli()
self.login()
@property
def chrome_options(self):
chrome_options = Options()
if sys.platform == "win32":
chrome_options.add_argument("--profile-directory=Default")
chrome_options.add_argument("--user-data-dir=C:/Temp/ChromeProfile")
else:
chrome_options.add_argument("start-maximized")
chrome_options.add_argument("--user-data-dir=./User_Data")
return chrome_options
def cli(self):
"""
LOGGER settings [nCKbr]
"""
handler = logging.StreamHandler()
handler.setFormatter(
logging.Formatter(
"%(asctime)s - %(name)s -- [%(levelname)s] >> %(message)s"
)
)
LOGGER.addHandler(handler)
LOGGER.setLevel(logging.INFO)
def open_window(self):
self.browser.get(self.BASE_URL)
time.sleep(2)
self.browser.maximize_window()
def login(self):
xpath = "/html/body/header/nav/div/div/div/button[2]"
email = "/html/body/div[1]/main/div[2]/div[2]/div/div[2]/div/div/form/div[1]/div[2]/input"
password = "/html/body/div[1]/main/div[2]/div[2]/div/div[2]/div/div/form/div[2]/div[2]/input"
login = "/html/body/div[1]/main/div[2]/div[2]/div/div[2]/div/div/form/div[3]/div/button"
signin_btn = self.wait.until(
EC.presence_of_element_located(
(
By.XPATH,
xpath,
)
)
)
signin_btn.click()
email_field = self.wait.until(
EC.presence_of_element_located(
(
By.XPATH,
email,
)
)
)
time.sleep(2)
email_field.send_keys("[email protected]")
password_field = self.wait.until(
EC.presence_of_element_located(
(
By.XPATH,
password
)
)
)
password_field.send_keys("xxxx")
login_btn = self.wait.until(
EC.presence_of_element_located(
(
By.XPATH,
login
)
)
)
login_btn.click()
automate.py
from main import BrowserAutomation
automate = BrowserAutomation()
But whenever I run the automate.py file I get the following error:
(venv) PS C:UsersAdministratorDesktope-ritaE-rita> python automate.py
Traceback (most recent call last):
File "C:UsersAdministratorDesktope-ritaE-ritaautomate.py", line 3, in <module>
automate = BrowserAutomation()
^^^^^^^^^^^^^^^^^^^
File "C:UsersAdministratorDesktope-ritaE-ritamain.py", line 19, in __init__
browser = webdriver.Chrome(
^^^^^^^^^^^^^^^^^
TypeError: WebDriver.__init__() got multiple values for argument 'options'
What should I add or edit in my code for the chrome window to be open successfully?
1