我正在尝试'接受'一个简单的模态警报(只有OK按钮的屏幕弹出窗口)但是driver.switch_to_alert()中的switch_to_alert()正在接收删除线(在pycharm上)。
我正在使用OpenPyxl的数据驱动测试脚本。我写了一个条件,从电子表格中获取用户名和密码,并将其添加到网页上登录。如果登录成功,则代码正常工作,但“其他”条件(即,如果登录失败失败)由于弹出警报而给我提出问题,我不知道如何关闭它。
我想了解为什么driver.switchto alert()正在接收删除线以及如何修复。
我在'else'条件“driver.switch_to_alert()”中添加了以下行,但是switch_to_alert()虽然有一个打击。
如果我将driver.switch_to_alert()移动到行driver = self.driver上方,则不会发生删除线。
或者,如果我只是在else条件下编写switch_to_alert(),那么在调用else条件时代码就会失败。我得到的错误消息是selenium.common.exceptions.UnexpectedAlertPresentException:警告文本:无消息:被解雇的用户提示对话框:用户或密码无效
我也尝试了很难(回到主页)也失败了。
def test_login_valid(self):
driver = self.driver
driver.get('http://www.demo.guru99.com/V4/')
#count how many rows in spreadsheet and add read username and password
#and add to www.demo.guru99.com login
for r in range(2, rows + 1):
username = spreadsheet.readData(path, "Sheet1", r, 1)
password = spreadsheet.readData(path, "Sheet1", r, 2)
login.enter_username(username)
login.enter_password(password)
login.click_login()
#If login is successful then pass test
if driver.title == "Guru99 Bank Manager HomePage":
print("test is passed")
spreadsheet.writeData(path, "Sheet1", r, 3, "test passed")
#return back to home page - i dont think i should need this line???
driver.get('http://www.demo.guru99.com/V4/')
else:
print("test failed")
spreadsheet.writeData(path, "Sheet1", r, 3, "test failed")
#accept the alert popup and close - python is striking though the bold
driver.switch_to_alert().accept() # error shown on this line
我希望当登录时的用户名或密码错误时,“其他”条件应该能够在警报通知上选择“确定”。这将关闭弹出窗口并导致测试返回主页。
几点:
switch_to_alert()
已被弃用,所以你应该使用switch_to().alert
代替。您可以在Python click button on alert找到相关的讨论。WebDriverWait
并将expected_conditions
子句设置为alert_is_present,如下所示:
WebDriverWait(driver, 5).until(EC.alert_is_present).accept()
您可以在Why switching to alert through selenium is not stable?找到相关的讨论根据@DebanjanB
的建议,我用以下内容更改了代码(我只包含上下文所需的代码)。但是,我收到以下错误:
selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: None
Message: Dismissed user prompt dialog: User or Password is not valid
使用Pycharm时要做的另一个评论。当您在代码中看到“alert”一词时,它会以黄色突出显示。 Pycharm建议Statement似乎没有效果检查信息:此检查检测语句没有任何影响。我不确定这意味着什么。
from selenium import webdriver
from selenium.webdriver.common.by import By # **This is greyed out in pycharm. is this** correct?
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def test_login_valid(self):
driver = self.driver
driver.get('http://www.demo.guru99.com/V4/')
login = LoginPage(driver)
for r in range(2, rows + 1):
#inserts username and password from spreadsheet - if username/password does not login the else condition is called and alter is raised. I'm trying to close this popup alert.
if driver.title == "Guru99 Bank Manager HomePage":
print("test is passed")
spreadsheet.writeData(path, "Sheet1", r, 3, "test passed")
driver.get('http://www.demo.guru99.com/V4/')
else:
print("test failed")
spreadsheet.writeData(path, "Sheet1", r, 3, "test failed")
# This line should close the alert but appears to cause error
**WebDriverWait(driver, 300).until(EC.alert_is_present).accept**