Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23276

open chrome tab to a url with Selenium ChromeDriver

$
0
0

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 sysimport loggingimport timefrom selenium import webdriverfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.chrome.options import Optionsfrom webdriver_manager.chrome import ChromeDriverManagerfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver.support.ui import WebDriverWaitLOGGER = 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("xxxx@gmail.com")        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 BrowserAutomationautomate = BrowserAutomation()

But whenever I run the automate.py file I get the following error:

(venv) PS C:\Users\Administrator\Desktop\e-rita\E-rita> python automate.pyTraceback (most recent call last):  File "C:\Users\Administrator\Desktop\e-rita\E-rita\automate.py", line 3, in <module>    automate = BrowserAutomation()               ^^^^^^^^^^^^^^^^^^^  File "C:\Users\Administrator\Desktop\e-rita\E-rita\main.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?


Viewing all articles
Browse latest Browse all 23276

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>