想了解为什么 switch_to_alert() 收到删除线以及如何修复 [英] Would like to understand why switch_to_alert() is receiving a strikethrough and how to fix
问题描述
我正在尝试接受"一个简单的模式警报(一个只有 OK 按钮的屏幕弹出窗口),但是 driver.switch_to_alert() 中的 switch_to_alert() 正在接收删除线(在 pycharm 上).
I'm trying to 'accept' a simple modal alert (an onscreen popup with only OK button) but switch_to_alert() in driver.switch_to_alert() is receiving a strikethrough (on pycharm).
我在 OpenPyxl 中使用数据驱动测试脚本.我写了一个条件,从电子表格中获取用户名和密码,并将其添加到网页登录.如果登录成功,则代码有效,但其他"条件(即,如果登录失败)由于弹出警报而给我带来问题,我不知道如何关闭它.
I'm using data driven testing script with OpenPyxl. I have written a condition that takes username and password from spreadsheet and adds it to login on webpage. If login is successful the code is work but the 'else' condition (ie. if fails login fails) is giving me issues due to the popup alert and I'm not sure how to close it.
我想了解为什么 driver.switch_to_alert() 收到罢工通知以及如何解决.
I would like to understand why driver.switch_to_alert() is receiving a strikethough and how to fix.
我在else"条件driver.switch_to_alert()"中添加了以下行,但 switch_to_alert() 有一个罢工.
I have added the following line at in the 'else' condition "driver.switch_to_alert()" but switch_to_alert() has a strike though.
如果我将 driver.switch_to_alert() 移动到 driver = self.driver 行上方,则删除线不会发生.
If i move driver.switch_to_alert() to above the line driver = self.driver then the strikethrough does not occur.
或者,如果我简单地在 else 条件中编写 switch_to_alert() ,则没有罢工,但是当调用 else 条件时代码会失败.我得到的错误信息是selenium.common.exceptions.UnexpectedAlertPresentException:警报文本:无消息:关闭的用户提示对话框:用户或密码无效
Alternately if i simply write switch_to_alert() in the else condition there is no strikethough but the code fails when the else condition is called. The error message i get is selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: None Message: Dismissed user prompt dialog: User or Password is not valid
我也尝试了一次硬获取(返回主页),但也失败了.
I also tried a hard get(back to home page) which also failed.
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
我希望当登录时的用户名或密码错误时,其他"条件应该能够在警报通知中选择确定".这将关闭弹出窗口并导致测试返回主页.
I expect when username or password on login is wrong then the "else" condition should be able to select "ok" on the alert notification. This will close the popup and result in test returning back to home page.
推荐答案
几点:
switch_to_alert()
已被弃用,因此您应该使用switch_to().alert
.您可以在 Python 单击警报按钮中找到相关讨论.根据最佳实践,在切换到警报时,您需要使用
WebDriverWait
并将expected_conditions
子句设置为 alert_is_present 如下:
switch_to_alert()
has been deprecated so you should useswitch_to().alert
instead. You can find a relevant discussion in Python click button on alert.As per best practices, while switching to an alert, you need to use
WebDriverWait
withexpected_conditions
clause set to alert_is_present as follows:
WebDriverWait(driver, 5).until(EC.alert_is_present).accept()
您可以在中找到相关讨论为什么通过 selenium 切换到 alert 不稳定?
这篇关于想了解为什么 switch_to_alert() 收到删除线以及如何修复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!