如何使用Python在Selenium中测试警报/弹出窗口

如何使用Python在Selenium中测试警报/弹出窗口,python,python-3.x,selenium,Python,Python 3.x,Selenium,我被我的自动化卡住了,当我输入错误的用户名和密码时,它会弹出“无法登录。尝试其他用户名” 现在需要测试文本为“无法登录。请尝试其他用户名”的警报或弹出窗口(不确定是否为警报或弹出窗口)。如果找到文本,则测试通过。如果没有,则测试必须失败 注意:只需添加,在关闭警报/弹出窗口之前,我们无法访问html dom。您需要声明您拥有预期的内部,然后关闭警报: from selenium.webdriver.support.ui import WebDriverWait from selenium.web

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

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

注意:只需添加,在关闭警报/弹出窗口之前,我们无法访问html dom。您需要声明您拥有预期的内部,然后关闭警报:

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()

这是浏览器弹出/经典js警报吗?你能给我看一下截图吗?谢谢。@alecxe我附上了这张照片。抱歉,需要划掉ip。我收到此错误。\u>alert=self.sh.getDriver()。请切换到.alert()类型错误:“alert”对象不是callable@user3174886当然,对不起,我的错误,现在已修复。我将删除附件。这与工作有关。
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()