如何使用Python测试Selenium中的Alert/Popup

2024-05-18 12:24:24 发布

您现在位置:Python中文网/ 问答频道 /正文

当我输入错误的用户名和密码时,它会弹出“无法登录”的提示。尝试其他用户名“

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

现在需要测试警报或弹出窗口(不确定这是一个警报或弹出窗口),其中有文本“无法登录。尝试不同的用户名”。如果找到了文本,那么测试就通过了。如果没有,那么测试就失败了。在

在注意:只是想要另外,在关闭警报/弹出窗口之前,我们无法访问htmldom


Tags: thetest文本self密码defdriversh
1条回答
网友
1楼 · 发布于 2024-05-18 12:24:24

您需要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()

相关问题 更多 >

    热门问题