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.