我有代码(python / selenium):
# Alert start
WebDriverWait(self.driver, 5).until(EC.alert_is_present())
self.driver.switch_to_alert().dismiss()
# Alert end第一行在浏览器中等待警报,第二行按下“取消”按钮关闭此窗口。它工作得很好。我决定创建两个函数。
def alertIsPresent(self, timeout=10):
WebDriverWait(self.driver, timeout).until(EC.alert_is_present())
def alertDismiss(self):
alert = self.driver.switch_to_alert()
alert.dismiss()我把这个函数叫做:
PageObject.alertIsPresent()
PageObject.alertDismiss()最后一个")“加了下划线,因为”参数自填“...我是python的新手,你能给我一些建议吗?
pageObjectClass:
class PageObject:
def __init__(self, driver, xpathLocator):
self.driver = driver
self.locator = xpathLocator
self.wait = WebDriverWait(self.driver, 100)
def waitElementToBePresent(self, timeout=10):
WebDriverWait(self.driver, timeout).until(
EC.visibility_of_element_located((By.XPATH, self.locator)))
def elementIsPresent(self):
return EC.visibility_of_element_located((By.XPATH, self.locator))
def alertIsPresent(self, timeout=10):
WebDriverWait(self.driver, timeout).until(EC.alert_is_present())
def alertDismiss(self):
alert = self.driver.switch_to_alert()
alert.dismiss()发布于 2020-02-05 21:48:58
您还没有提到Selenium的python客户端的版本信息。
但是,根据当前的实现和documentation
switch_to
SwitchTo: an object containing all options to switch focus into
Usage: driver.switch_to.alert因此,您需要有效地替换以下代码行:
alert = self.driver.switch_to_alert()通过以下方式:
alert = self.driver.switch_to.alert发布于 2020-02-05 22:10:33
不需要使用两个函数,EC.alert_is_present()将返回警报
def alertIsPresent(self, timeout=10):
return WebDriverWait(self.driver, timeout).until(EC.alert_is_present())您还需要从类实例调用它,而不是从类型
PageObject(driver).alertIsPresent().dismiss()https://stackoverflow.com/questions/60076192
复制相似问题