gpt4 book ai didi

python - 如何使用 Python 在 Selenium 中测试 Alert/Popup

转载 作者:行者123 更新时间:2023-11-28 21:09:42 25 4
gpt4 key购买 nike

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

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

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

注意:只是想补充一点,在关闭警告/弹出窗口之前,我们无法访问html dom

最佳答案

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

关于python - 如何使用 Python 在 Selenium 中测试 Alert/Popup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37866584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com