Python / Selenium:切换到警报并验证其中的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18477689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python / Selenium : Switch to an alert and verify the text within
提问by Dave
My issue: Being able to detect an alert box has been opened, verify the text inside the alert, confirm the text and then close the alert. Then, go back and correct the invalid email.
我的问题:能够检测到警报框已打开,验证警报内的文本,确认文本,然后关闭警报。然后,返回并更正无效的电子邮件。
My test is one for registering a user. I need to validate when someone might input incorrect data or mismatched data. In this case, I am entering an email and verifying the email first entered. Should they have a mismatch, a popup will appear alerting the user to check the email addresses they have entered and that they match. So far all I can get is an error.
我的测试是注册用户的测试。我需要验证何时有人可能输入不正确的数据或不匹配的数据。在这种情况下,我正在输入电子邮件并验证第一次输入的电子邮件。如果它们不匹配,则会出现一个弹出窗口,提醒用户检查他们输入的电子邮件地址是否匹配。到目前为止,我所能得到的只是一个错误。
Error:
错误:
E UnexpectedAlertPresentException: Message: u'Modal dialog present' ; Stacktrace: Method nsCommandProcessor.prototype.execute threw an error in file:///var/folders/1j/x3cxkrqn3sscdyq0t36169hr0000gn/T/tmpTCqMgm/extensions/[email protected]/components/command_processor.js
E UnexpectedAlertPresentException: Message: u'Modal dialog present'; Stacktrace:方法 nsCommandProcessor.prototype.execute 在 file:///var/folders/1j/x3cxkrqn3sscdyq0t36169hr0000gn/T/tmpTCqMgm/extensions/[email protected]/components/command_processor.js 中引发错误
I thought my code would handle this, but nope. If someone could point out my, obvious to you, mistake I would be grateful.
我以为我的代码会处理这个问题,但不是。如果有人能指出我的,对你来说显而易见的错误,我将不胜感激。
My entire test code:
我的整个测试代码:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re
class ChallengeTests(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(5)
self.base_url = "https://www.testpage.com"
self.verificationErrors = []
self.accept_next_alert = True
# SIGN UP NEW USER
def test_00_sign_up(self):
driver = self.driver
driver.get(self.base_url + "/")
driver.find_element_by_id("remail").send_keys("[email protected]")
driver.find_element_by_id("remail_confirm").send_keys("[email protected]")
driver.find_element_by_id("next").click()
alert = self.driver.switch_to_alert()
alert = self.assertTrue(self.is_text_present("The email addresses you entered"))
driver.find_element_by_id("remail_confirm").send_keys("[email protected]")
driver.find_element_by_id("registration_button").click()
# NON TESTS
def is_element_present(self, how, what):
try:
self.driver.find_element(by=how, value=what) # to find page elements
except NoSuchElementException, e: return False
return True
def is_text_present(self, text):
try:
body = self.driver.find_element_by_tag_name("body") # find body tag element
except NoSuchElementException, e: return False
return text in body.text # check if the text is in body's text
def is_alert_present(self):
try:
self.driver.switch_to_alert()
except NoAlertPresentException, e:
return False
def close_alert_and_get_its_text(self):
try:
alert = self.driver.switch_to_alert()
alert_text = alert.text
if self.accept_next_alert:
alert.accept()
else:
alert.dismiss()
return alert_text
finally: self.accept_next_alert = True
return True
def tearDown(self):
self.driver.quit()
self.assertEqual([], self.verificationErrors)
采纳答案by Amey
Try waiting for the alert to appear, before directly switching to it.
尝试等待警报出现,然后直接切换到它。
If that does not work, I have a feeling that the popup actually is a weblement and not a JS alert. if that is the case try looking for a selector using the browser developer tools and directly interact with it.
如果这不起作用,我有一种感觉,弹出窗口实际上是一个网页,而不是一个 JS 警报。如果是这种情况,请尝试使用浏览器开发人员工具寻找选择器并直接与之交互。
回答by user3174886
On newer version of Python(3.4)
在较新版本的 Python(3.4) 上
def is_alert_present(self):
try:
self.driver.switch_to_alert()
except NoAlertPresentException:
return False