【问题标题】:Would like to understand why switch_to_alert() is receiving a strikethrough and how to fix想了解为什么 switch_to_alert() 收到删除线以及如何修复
【发布时间】:2019-09-10 23:34:48
【问题描述】:

我正在尝试“接受”一个简单的模式警报(屏幕上只有 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:警报文本:无 消息:关闭的用户提示对话框:用户或密码无效

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

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”条件应该能够在警报通知上选择“ok”。这将关闭弹出窗口并导致测试返回主页。

【问题讨论】:

  • 如果您将鼠标悬停在删除线文本上,它会显示什么内容?
  • DebenjanB 指出它已被弃用。我实施了他建议的解决方案,但现在我遇到了另一个问题(见下文)。

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


【解决方案1】:

几点:

【讨论】:

  • 我将 driver.switch_to_alert().accept() 更改为 WebDriverWait(driver, 5).until(EC.alert_is_present).accept() 并从 selenium.webdriver.support.ui import WebDriverWait 添加from selenium.webdriver.support import expected_conditions as EC 但它仍然失败。我收到此错误消息。 selenium.common.exceptions.UnexpectedAlertPresentException:警报文本:无消息:关闭的用户提示对话框:用户或密码无效。这是说警报不是警报吗?
  • 我有 selenium webdriver 版本 3.141.0 和 python 3.7
【解决方案2】:

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

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

使用 Pycharm 时要提出的另一条评论。当您在代码中看到“警报”一词时,它会以黄色突出显示。 Pycharm 提示 Statement 似乎没有效果 检查信息:此检查检测到没有任何影响的语句。我不确定这意味着什么。

    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

【讨论】:

    猜你喜欢
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    • 2017-10-17
    • 2012-05-20
    • 1970-01-01
    • 1970-01-01
    • 2018-11-01
    相关资源
    最近更新 更多