How to deal with popup messages selenium webdriver - Java - java

I am currently working on creating a script that will test the functionality of a website. Currently i'm blocked by a popup message that appears when certain conditions are met, when that popup message appears my script fails, if i add something like driver.findElement(By.xpath("//div[4]/div/div/div/div/div/button")).click();
the script fails when the popup does not appear.
My question is: Is there a way to check "If the button exists then click button x if not move forward". I know after what action (click) the message appears/does not appear.
Keep in mind that i'm now learning java and selenium.

Banu
You are right, You should use if else condition.
Because you exactly know in which condition pop up appears and you can use pop up text in if condition.
By
driver.switchTo().alert().getText();

Use Alert class to handle Popups
Alert alert = driver.switchTo().alert();
alert.accept();

You can use explicit wait for presence of button:-
WebDriverWait wait = new WebDriverWait(driver,10)
WebElement btn = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("---")));
Then to dismiss/accept the alert message use following code:-
driver.switchTo().alert().dismiss();
driver.switchTo().alert().accept();

Related

Using 'Alert' Interface in Selenium Java for alert conformation not working as expected

I used below selenium codes to click "OK" alert box, But It is not identifying, I dont knw wht's the reason, Can anyone help to solve this Issue ?
Element image:
Code trials:
Alert click_on_update_button_Htwelveyes = driver.switchTo().alert();
click_on_update_button_Htwelveyes.accept();
}
Console Output:
org.openqa.selenium.NoAlertPresentException: no such alert
(Session info: chrome=103.0.5060.134)
Alert
An alert box is used if the user wants to make sure information comes through to the user. The user will have to click on OK to agree or Cancel to deny the information.
This usecase
The following element doesn't seems to be an alert but a Modal Dialog Box :
In such cases you need to locate the element with the Modal Dialog Box to locate the element with text as Yes or No and invoke click() on the respective element of youe choice.

Unable to click on 'OK' button in the web alert

Am trying to click on 'OK' button in the attached popup using selenium webdriver alert interface.
When I inspected the 'OK button using accessibility inspector, it has shown the below properties.
target.processes()["Google Chrome"].frontWindow().images()["Chrome critical alert"].click()
target.processes()["Google Chrome"].frontWindow().buttons()["OK"].click()
So, I have tried webdriver alert to click on 'OK', but it didnt work.
Alert alert = driver.switchTo().alert();
alert.accept();
Have spent enough time on this but couldnt find a solution.Appreciate any help here !
That doesn't look like a browser "alert" box to me, so the Alert method won't find it. It also doesn't look like a "popup" (a separate desktop window) in the usual sense. I'm guessing from your description that it's a conventional HTML element on the page. Therefore, you need to find the OK button by usual Selenium element-finding techniques and then click() on the element.

In Selenium java how to handle the pop up

I use selenium 2 and the browser version is IE 11. I face issue while handling with pop up.
Scenario 1:
driver.findElement(By.id("I create modal window")).click();
Set<String> windows = driver.getWindowHandles();
System.out.println(windows.size());
I get the output as 2.
Scenario: 2 (with the same concept)
driver.findElement(By.id("I create pop up")).click();
Set<String> windows = driver.getWindowHandles();
System.out.println(windows.size());
I get the output as 1.
I could not switch to the pop-up. Some times the pop-up response like not connected and moreover the size of the popup is way smaller than what it should be.
What would be the problem
Note : The element id's are not real one
You have to first check its window or alert.
Simply try to inspect element in that popup, if you are able to inspect elements in popup then its window so use switch to window command, if not then it is alert. So use switch to alert command (which provided in another answer)
From your question it looks like you wanted to switch to a pop - up.
Window handler is used to switch between browser windows.
Alert handler is used to switch between the pop - up messages.
If you wanted to accept/yes/ok the alert:
Alert alert = driver.switchTo().alert();
alert.accept();
If you wanted to dismiss/no/cancel the alert:
Alert alert = driver.switchTo().alert();
alert.dismiss();
There are different types of alerts and it should be handled differently as given below.
Windows type:
if your popup opened in a new window, you can handle it using get window handle and switch to window methods.
driver.selectElement(By.id("I create modal window")).click();
Set<String> windows = driver.getWindowHandles();
System.out.println(windows.size());
for( String window : windows)
driver.switchToWindow(window);
Javascript alert:
if the popup is javascript type. you can use switch to alert method.
Alert alert = driver.switchTo().alert();
DOM Element type:
sometimes the popup is like DOM Element, created dynamically or hidden until popup. It can be handled using find elemeent method.
driver.findElement(locator).Click();
I haven't fix my problem, but I found interesting things : when i open a popup, it is not connected to the current session if i open my web site with Selenium, even if don't ask Selenium to open my popup but i do it my self. If id do :
driver.quit()
the Main window will be close but not the popup

Selenium Java Chrome send enter key while the elements on the page are disabled

I need to close a dialog box that pops up after I had selected 'Save' by clicking 'Ok', however none of the elements on the page as well as dialog box can be inspected by right clicking after the pop-up (tried F12 doesn't help).
Alternatively, to close the dialog box enter key could be given, however I'm unable to send the enter key as listed below.
Actions action = new Actions(driver); //attempt 1
action.sendKeys(Keys.RETURN);
action.sendKeys(Keys.RETURN).perform(); //attempt 2
Both actions does not close the dialog box. I had performed the driver switch as well. In addition to the dialog box pop-up there's also an outlook email pop-up could this create an issue in identifying the alert pop-up? Kindly advise on how enter key could be passed to close the dialog box.
Found the issue, it was due to Thread.sleep(3000) that I had used after the click, guess this caused it to miss the alert from being recognized. I might be worng as well. Thanks for all your help!
As none of the elements can be inspected by right clicking after the pop-up which indicates its an Alert which is Javascript generated and you can use the following line of code :
Alert myAlert = new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent());
myAlert.accept();
Update A
As per the comment update :
Observation : In addition to the alert msg there's also outlook mail also pops up
Conclusion : If outlook mail pops up possibly it's not an Alert as suspected. You should be able to track the element within the HTML DOM.
Update B
As per the comment update :
Observation : there are 2 thing happens when i click on 'Save': [1]: Outlook email pop-up [2]: Alert notification on the saved page stating the records are being updated
Conclusion : Sounds like two window_handles opening up, treat them as window_handles.
Solution 1 : Try switching to the pop up and handle it.
new WebDriverWait(driver, 15).until(ExpectedConditions.alertIsPresent());
driver.switchTo().alert().accept();
Solution 2 : If you want to go with Keyboard keys.
Actions action = new Actions(driver);
new WebDriverWait(driver, 15).until(ExpectedConditions.alertIsPresent());
action.sendKeys(Keys.RETURN).perform();
In both cases, use 'wait' until you get an alert.
First, check the dialog box that pops up after you click 'Ok' is JavaScript Alert or anything else?
If the dialog is JavaScript Alert then try below code.
new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.accept();

How wait for alert box to perform the action in Selenium?

I am pressing a cancel button than according to my code it is checking some text. In Chrome and Firefox it is working fine but in IE it is taking time to perform operation on alert box but code moves to next line.
So i want some code that stop till the operation is preformed on alert box and than it goes to next Step. I am doing automation testing using selenium.
Please find piece of code:
Alert al = driver.switchTo().alert();
al.accept();
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//*[#id='content-body-full']/p[1]")));
assertEquals("Your cancellation request has been successfully executed.",driver.findElement(By.xpath(".//*[#id='content-body-full']/p[1]")).getText().toString());
You want to wait until the alert is present before switching to it, similar to your 3rd row. Reference.
EDIT: try:
new WebDriverWait(driver, 60)
.ignoring(NoAlertPresentException.class)
.until(ExpectedConditions.alertIsPresent());
Alert al = driver.switchTo().alert();
al.accept();

Categories

Resources