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

Would like to understand why switch_to_alert() is receiving a strikethrough and how to fix

我正在尝试 'accept' 一个简单的模态警报(屏幕上只有确定按钮的弹出窗口)但是 driver.switch_to_alert() 中的 switch_to_alert() 正在接收删除线(在pycharm)。

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

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

我也试过hard 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

我希望当登录时的用户名或密码错误时 "else" 条件应该能够 select "ok" 在警报通知上。这将关闭弹出窗口并导致测试返回主页。

几点:

  • switch_to_alert() has been deprecated so you should use switch_to().alert instead. You can find a relevant discussion in Python click button on alert.
  • 根据最佳实践,在切换到警报时,您需要使用 WebDriverWait 并将 expected_conditions 子句设置为 alert_is_present,如下所示:

    WebDriverWait(driver, 5).until(EC.alert_is_present).accept()
    

    您可以在

  • 中找到相关讨论

根据 @DebanjanB 的建议,我已将我的代码更改为以下内容(我只包含上下文所需的代码)。 但是,我收到以下错误:

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

使用 Pycharm 时要发表的另一条评论。当您在代码中看到单词 "alert" 时,它以黄色突出显示。 Pycharm advises 声明似乎没有效果 检查信息:此检查检测语句没有任何影响。我不确定这是什么意思。

    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