我正在创建一个自动化程序,并且创建了一个浏览器自动化类来处理 Chrome 浏览器的自动化和打开。我已将代码分成两个文件,即 main.py (包含所有类和方法)和automate.py (使用创建的类和方法)。下面是我的两个文件的源代码。
我想运行代码,以便使用提供的 URL 自动打开 Chrome 选项卡。
主.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()
自动化.py
from main import BrowserAutomation
automate = BrowserAutomation()
但是每当我运行automate.py文件时,我都会收到以下错误:
(venv) PS C:\Users\Administrator\Desktop\e-rita\E-rita> python automate.py
Traceback (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'
我应该在代码中添加或编辑什么才能成功打开 chrome 窗口?
删除这个逗号
browser = webdriver.Chrome(
ChromeDriverManager().install(),
options=self.chrome_options,
^ remove this
)
它应该看起来像
browser = webdriver.Chrome(
ChromeDriverManager().install(),
options=self.chrome_options
)