【问题标题】:How to test Alert/Popup in Selenium using Python如何使用 Python 在 Selenium 中测试警报/弹出窗口
【发布时间】:2016-06-16 18:18:05
【问题描述】:

我的自动化卡住了,当我输入错误的用户名和密码时,它会弹出“无法登录。尝试不同的用户名”

def test_logonWrongUserName(self):
    self.setUpClass() # Initialize the driver 
    self.sh.navigateToUrl("http://test.com")
    self.sh.member_Login("Foo", "asd") 

现在需要测试带有文本“无法登录。尝试不同的用户名”的警报或弹出窗口(不确定它是警报还是弹出窗口)。如果找到文本,则测试通过。如果不是,则测试必须失败。

注意:只是想添加,在关闭警报/弹出窗口之前,我们无法访问 html dom

【问题讨论】:

  • 这是浏览器弹出/经典 js 警报吗?你能显示截图吗?谢谢。
  • @alecxe 我附上了图片。对不起,需要划掉 ip。

标签: python python-3.x selenium


【解决方案1】:

你需要wait for the alert to be present,断言你有预期的text,然后关闭/accept警报:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

wait = WebDriverWait(driver, 10)
wait.until(EC.alert_is_present())

alert = driver.switch_to.alert
assert "Unable to login. Try different username" in alert.text
alert.accept()

【讨论】:

  • 我收到此错误_> alert = self.sh.getDriver().switch_to.alert() TypeError: 'Alert' object is not callable
  • @user3174886 当然,对不起,我的错误,现在修正了。
  • 我要删除附件。它与工作有关。
猜你喜欢
  • 1970-01-01
  • 2020-09-06
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 2015-03-06
  • 2019-01-17
  • 1970-01-01
  • 2015-12-16
相关资源
最近更新 更多