Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python/Selenium:切换到警报并验证其中的文本_Python_Selenium - Fatal编程技术网

Python/Selenium:切换到警报并验证其中的文本

Python/Selenium:切换到警报并验证其中的文本,python,selenium,Python,Selenium,我的问题:能够检测到警报框已打开,验证警报内的文本,确认文本,然后关闭警报。然后,返回并更正无效电子邮件 我的测试是注册一个用户。我需要验证是否有人输入了不正确的数据或不匹配的数据。在这种情况下,我正在输入一封电子邮件并验证第一次输入的电子邮件。如果他们不匹配,将出现一个弹出窗口,提醒用户检查他们输入的电子邮件地址是否匹配。到目前为止,我只能得到一个错误 错误: E UnexpectedAlertPresentException:消息:u'Modal对话框 现在';Stacktrace:方法ns

我的问题:能够检测到警报框已打开,验证警报内的文本,确认文本,然后关闭警报。然后,返回并更正无效电子邮件

我的测试是注册一个用户。我需要验证是否有人输入了不正确的数据或不匹配的数据。在这种情况下,我正在输入一封电子邮件并验证第一次输入的电子邮件。如果他们不匹配,将出现一个弹出窗口,提醒用户检查他们输入的电子邮件地址是否匹配。到目前为止,我只能得到一个错误

错误:

E UnexpectedAlertPresentException:消息:u'Modal对话框 现在';Stacktrace:方法nsCommandProcessor.prototype.execute 出错file:///var/folders/1j/x3cxkrqn3sscdyq0t36169hr0000gn/T/tmpTCqMgm/extensions/fxdriver@googlecode.com/components/command_processor.js

我以为我的代码可以处理这个问题,但没有。如果有人能指出我的错误,我将不胜感激

我的整个测试代码:

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("foobar@me.com")
        driver.find_element_by_id("remail_confirm").send_keys("bar@me.com")
        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("foobar@me.com")
        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)

在直接切换到警报之前,请尝试等待警报出现

如果这不起作用,我感觉弹出窗口实际上是一个WebElement,而不是JS警报。如果是这种情况,请尝试使用浏览器开发工具寻找选择器,并直接与之交互。

在较新版本的Python(3.4)上


您是否尝试过注释隐式等待并以这种方式运行您的测试?您是正确的,这是一个webelement。但是,我无法获取元素的属性以与之交互。一旦我有了它,我就可以与它进行交互,例如:在6.3下,如果你能告诉我如何获取webelement的ID/名称,那就太好了。生成警报时,请尝试使用Firebug或Chrome Developer工具检查该元素。这应该是一个好的开始。一旦找到元素,就不需要6.3模块下列出的交互方法。我(萤火虫)看不见它。我相信这是因为我不知道该去哪里找查尔斯。非常感谢戴夫!你救了我一个下午!:)PS:您尝试过使用get_alert()吗?这对我有用!请详细说明您的答案。当我尝试在较新版本的Python上粘贴代码时,它给了我一个错误。然后我尝试了这个方法,它工作正常。
def is_alert_present(self):
    try:
        self.driver.switch_to_alert()
    except NoAlertPresentException: 
        return False