Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.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 webdriver:不检测生成的警报,也不给出错误_Python_Testing_Automation_Selenium Webdriver_Alert - Fatal编程技术网

Python Selenium webdriver:不检测生成的警报,也不给出错误

Python Selenium webdriver:不检测生成的警报,也不给出错误,python,testing,automation,selenium-webdriver,alert,Python,Testing,Automation,Selenium Webdriver,Alert,*我正在使用Python中的Selenium Webdriver对网页进行测试用例自动化。 *我需要验证网页上生成的警报消息。 *当我试图处理警报时,使用Webdriver,它会说找不到警报。如果我不处理警报,它甚至不会给出任何错误消息(如生成的未接受警报)。 *请说明为什么Webdriver未检测到警报 Code: from selenium import webdriver from selenium.webdriver.common.by import By from selen

*我正在使用Python中的Selenium Webdriver对网页进行测试用例自动化。
*我需要验证网页上生成的警报消息。
*当我试图处理警报时,使用Webdriver,它会说找不到警报。如果我不处理警报,它甚至不会给出任何错误消息(如生成的未接受警报)。
*请说明为什么Webdriver未检测到警报

Code:

from selenium import webdriver  
from selenium.webdriver.common.by import By  
from selenium.webdriver.common.keys import Keys  
from selenium.webdriver.support.ui import Select  
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re

class Tcgr1(unittest.TestCase):  
    def setUp(self):  
        self.driver = webdriver.Firefox()                             
        self.driver.implicitly_wait(30)   
        self.base_url = "https://colama5863.co5863domain.net/infracc/app/Workspace#VMs"
        self.accept_next_alert = True

    def test_tcgr1(self):
        driver = self.driver
        driver.find_element_by_link_text("Settings").click()
        driver.find_element_by_link_text("Group Mgmt").click()
        driver.find_element_by_xpath("//button[contains(text(), \"Create\")]").click()
        driver.find_element_by_name("groupname").clear()
        driver.find_element_by_name("groupname").send_keys("tcgr1_group")
        driver.find_element_by_xpath("//span[2]/span/button[contains(text(), \"Create\")]").click()
        for i in range(60):
            try:
                if self.is_alert_present(): break
            except: pass
            time.sleep(1)
        else: self.fail("time out")
        self.assertEqual("Group created successfully", self.close_alert_and_get_its_text())

    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e: return False
        return True

    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException, e: return False
        return True

    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

    def tearDown(self):
        self.driver.quit()


if __name__ == "__main__":     
   unittest.main()

Webdriver正在按预期工作。在不存在的警报窗口上执行操作时,NoAlertPresentException将引发。当切换到不存在的警报窗口时,它不会升高

因此,要使代码正常运行,需要对假定的警报执行操作:

def is_alert_present(self):
    try: 
        alert = self.driver.switch_to_alert()  # this doesn't raise Exception
        alert.text  # this DOES RISE Exception if there isn't an alert
        return True

    except NoAlertPresentException:
        return False

你能发布你正在使用的代码吗。您使用的是隐式等待函数还是更可靠的预期条件等待函数?请给出一些建议。。。我已经添加了测试用例的代码。