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

拈花ヽ惹草 提交于 2021-02-17 03:29:50

问题


I'm trying to 'accept' a simple modal alert (an onscreen popup with only OK button) but switch_to_alert() in driver.switch_to_alert() is receiving a strikethrough (on pycharm).

I'm using data driven testing script with OpenPyxl. I have written a condition that takes username and password from spreadsheet and adds it to login on webpage. If login is successful the code is work but the 'else' condition (ie. if fails login fails) is giving me issues due to the popup alert and I'm not sure how to close it.

I would like to understand why driver.switch_to_alert() is receiving a strikethough and how to fix.

I have added the following line at in the 'else' condition "driver.switch_to_alert()" but switch_to_alert() has a strike though.

If i move driver.switch_to_alert() to above the line driver = self.driver then the strikethrough does not occur.

Alternately if i simply write switch_to_alert() in the else condition there is no strikethough but the code fails when the else condition is called. The error message i get is selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: None Message: Dismissed user prompt dialog: User or Password is not valid

I also tried a hard get(back to home page) which also failed.

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

I expect when username or password on login is wrong then the "else" condition should be able to select "ok" on the alert notification. This will close the popup and result in test returning back to home page.


回答1:


A couple of points:

  • 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.
  • As per best practices, while switching to an alert, you need to use WebDriverWait with expected_conditions clause set to alert_is_present as follows:

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

    You can find a relevant discussion in Why switching to alert through selenium is not stable?




回答2:


Following the advice from @DebanjanB, I have changed my code with the following (i only include the code you need for context). However, I get the following error:

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

One other comment to make when using Pycharm. When you see the word "alert" in in the code it is highlighted in yellow. Pycharm advises Statement seems to have no effect Inspection info: This inspection detects statements without any effect. I'm not sure what this means.

    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



来源:https://stackoverflow.com/questions/55783153/would-like-to-understand-why-switch-to-alert-is-receiving-a-strikethrough-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!