Python Forum
Issue closing an Modal Alert (popup)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Issue closing an Modal Alert (popup)
#1
Hi All

I'm playing around with the following site (python 37/Selenium).

http://www.demo.guru99.com/V4/index.php

When i type in an incorrect password or username. I get a popup/alert.

I cant understand how to find element of the popup so i can select "ok" in the popup. I know about the find_element commands but when i open the inspector i cant see the elements of the popup.

If I understand the documentation correctly the following line would work
driver.switch_to_alert().accept without pointing it any particular element but
when i plug it into pycharm the following seems to happen driver.[strike]switch_to_alert[/strike]().alert() (i.e. strikethough in the switch_to_alert).

I played around with the line driver = driver.switch_to_alert(). It looks like it has strike though when i place it below the following line self.driver() but has no strikethough if i place it above the following line line self.driver() in line 3 (all in the same def function.)

In the code snippet below - i enter the username/password. If it is correct (ie line 18 it is passed) but if incorrect username or password (ie the else condition in line 21) i need to be able to press ok on the alert that popsup.

Can someone explain how i get around this?

   def test_login_valid(self):
        
        driver = self.driver
        driver.get('http://www.demo.guru99.com/V4/')
        path = "C://FireFoxProfile/login1.xlsx"

        rows = spreadsheet.getRowCount(path, 'Sheet1')

        login = LoginPage(driver)
#loop to extract username and password from spreadsheet and enter into website demo.guru
        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 username and Passwork then check page is loaded, else fail page
            if driver.title == "Guru99 Bank Manager HomePage":
                print("test is passed")
                spreadsheet.writeData(path, "Sheet1", r, 3, "test passed")
            else:
                print("test failed")
                spreadsheet.writeData(path, "Sheet1", r, 3, "test failed")
            # return to homepage and insert password/username for next row in spreadsheet
            driver.switch_to_alert().accept  #whats the issue with this line? I need to click OK on the # alert/popup
            
            
        time.sleep(2)
I would be so grateful if someone can point me in right direction.
Reply
#2
Please show enough code so it can be run.
Reply
#3
I added the files here, I'm a complete newbie with github/selenium so i hope it works
https://github.com/Kingleviathan/SeleniumPractice I'm really grateful for if you let me know what im doing wrong.

a couple points to note
1. You will need openpyxl installed
2. You will need to change line 14 and 15 in login.py to call your own browser
3. You will need to change line 25 location to call the spreadsheet
4. you need to run login.py


the code thats giving me issue is found in login.py - this line in paticulr
WebDriverWait(driver, 300).until(EC.alert_is_present).accept

here is the section
else:
                print("test failed")
                spreadsheet.writeData(path, "Sheet1", r, 3, "test failed")
            # return to homepage and insert password/username for next row in spreadsheet this line does not work
                WebDriverWait(driver, 300).until(EC.alert_is_present).accept
                time.sleep(5)
The error message i get is
 selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: None
Message: Dismissed user prompt dialog: User or Password is not valid
Reply
#4
Just downloaded the code and looking at it now 7:28 A.M. EDT
Reply
#5
Thanks so much. I'm really grateful. I'm sure your going to tell me it's the smallest thing. I really don't know enough.
Reply
#6
This is requiring more effort that I am willing to provide.
Suggest modifying code so password and username hard coded (use 'user', 'password' as goal is to make it fail)
also, don't close browser page, allow to be done manually.
Reply
#7
That's OK. I understand. I appreciate your effort. It didn't occur to me it might be complex. I thought it was just about fixing a single line.

Can you clarify what you mean with this?

Suggest modifying code so password and username hard coded (use 'user', 'password' as goal is to make it fail)

The goal of my exercise is to extract from the spreadsheet. I found the exercise online but unfortunately for me it's in java and not python.
Reply
#8
I mean for purposes of code for forum, eliminate (comment out) the spreadsheet and hardcode any user name and password

Quote:The goal of my exercise is to extract from the spreadsheet.
I thought it was:
Quote:I cant understand how to find element of the popup so i can select "ok" in the popup.
Reply
#9

Following suggestion from @Larz60+ I have simplified the code so username/password is hardcoded and no spreadsheet is referenced.

It looks to me like the accept alert is actually working with this script BUT something is happening that preventing me recording the test result. so for example if i successfully login then it will print "test is passed" but if i don't successfully login it does not print "test failed". Instead I get this error message

Traceback (most recent call last):
  File "C:/Users/David/PycharmProjects/POM/Login2.py", line 21, in <module>
    if driver.title == "Guru99 Bank Manager HomePage":
  File "C:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 342, in title
    resp = self.execute(Command.GET_TITLE)
  File "C:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 241, in check_response
    raise exception_class(message, screen, stacktrace, alert_text)
selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: None
Message: Dismissed user prompt dialog: User or Password is not valid
Here is my simplified script. You can cut and paste this script to test with
from selenium import webdriver
import unittest
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


profile_path = 'C:/FireFoxProfile'
profile = webdriver.FirefoxProfile(profile_path)
driver = webdriver.Firefox(firefox_profile=profile_path)
driver.implicitly_wait(2)
driver.get('http://www.demo.guru99.com/V4/')
driver.maximize_window()

username = "mngr189426" #correct password is "mngr189426"
password = "fail" #correct password is "hapEnyn"

driver.find_element_by_name("uid").send_keys(username)
driver.find_element_by_name("password").send_keys(password)
driver.find_element_by_name("btnLogin").click()
#if login is successful then check website is on right page and pass/fail test
if driver.title == "Guru99 Bank Manager HomePage":
    print("test is passed")
else:
    print("test failed")
    # return to homepage and insert password/username for next row in spreadsheet
    WebDriverWait(driver, 20).until(EC.alert_is_present).accept

if __name__ == '__main__':
    unittest.main(verbosity=2)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to use data from an API, to create an alert? PopFendi 8 3,800 Apr-22-2021, 02:21 PM
Last Post: PopFendi
  Monitor specific line of code and get alert Olimpiarob 0 1,523 Jul-08-2020, 10:06 AM
Last Post: Olimpiarob
  Python / Selenium Turning Off Alert graham23s 3 8,253 Aug-18-2019, 02:12 PM
Last Post: metulburr
  Selenium click on popup button??? GuJu 7 7,868 Jul-20-2019, 09:21 AM
Last Post: Nizam
  Getting error when accessing elements in a modal window of an webpage using selenium sumandas89 3 8,655 Jul-13-2018, 10:44 AM
Last Post: mlieqo
  How to access Modal window using Selenium Python sumandas89 0 4,203 Mar-30-2018, 08:21 AM
Last Post: sumandas89
  Generic If Popup Exists Close It Script digitalmatic7 1 2,480 Feb-18-2018, 07:24 AM
Last Post: metulburr
  popup login form in django uditvashisht 0 3,812 Jan-31-2018, 11:55 PM
Last Post: uditvashisht
  selenium bypass javascript popup box metulburr 6 8,405 Jun-02-2017, 07:15 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020