请参阅此类:
浏览器
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains as actions
class Browser:
driver = webdriver.Chrome("C:\\chromedriver.exe")
webDriverWait = WebDriverWait(driver, 60)
action = actions(driver)
def goto(self, url):
Browser.driver.maximize_window()
Browser.driver.get(url)
主页
from seleniumbase.Browser import Browser
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from seleniumbase.BaseActions import BaseActions
class HomePage:
url = "myURL"
def __init__(self):
self.usernametextbox = Browser.webDriverWait.until(ec.visibility_of_element_located((By.ID, "login_email")))
self.passwordtextbox = Browser.webDriverWait.until(
ec.visibility_of_element_located((By.CSS_SELECTOR, "tr[id=mockpass] input")))
self.loginbutton = Browser.webDriverWait.until(ec.element_to_be_clickable((By.NAME, "login")))
def getusernametextbox(self):
return self.usernametextbox
def getpasswordtextbox(self):
return self.passwordtextbox
def getloginbutton(self):
return self.loginbutton
def login(self):
self.getusernametextbox().send_keys("myuser@gmail.com")
self.getpasswordtextbox().send_keys("12345678")
self.getloginbutton().click()
如您所见,我将WebElements
定义为方法。
是这样做的写方法还是有更好的方法?
是这样做的写方法还是有更好的方法?
答案 0 :(得分:1)
请勿在{{1}}内定义每个元素,请尝试在下面使用:
__init__