想了解为什么switch_to_alert()收到删除线以及如何解决

时间:2019-04-21 13:26:05

标签: python-3.x selenium-webdriver alert webdriverwait

我试图“接受”一个简单的模式警报(仅带有OK按钮的屏幕弹出窗口),但是driver.switch_to_alert()中的switch_to_alert()收到删除线(在pycharm上)。

我在OpenPyxl中使用了数据驱动的测试脚本。我写了一个条件,该条件从电子表格中获取用户名和密码,并将其添加到网页登录中。如果登录成功,则代码可以正常工作,但是“其他”情况(即,如果登录失败失败)由于弹出警报而给我一些问题,我不确定如何关闭它。

我想了解为什么driver.switch_to_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:警报文本:无 消息:已关闭的用户提示对话框:用户或密码无效

我还尝试了一次艰难的get(返回首页),但也失败了。

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

我希望在登录时使用的用户名或密码错误时,“其他”条件应该能够在警报通知中选择“确定”。这将关闭弹出窗口,并导致测试返回到首页。

2 个答案:

答案 0 :(得分:0)

几点:

答案 1 :(得分:0)

根据@DebanjanB的建议,我对代码进行了以下更改(我仅包含上下文所需的代码)。 但是,出现以下错误:

selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: None
Message: Dismissed user prompt dialog: User or Password is not valid

使用Pycharm时要发表的另一条评论。当您在代码中看到“警报”一词时,将以黄色突出显示。 Pycharm建议声明似乎无效 检验信息:该检验检测到的语句没有任何影响。我不确定这是什么意思。

    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**

image of the alert