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

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.

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.

How to click on a confirm button inside a modal that's a pop up

I want to click on the "Confirm" button in the pop up inside a modal. But when I click on the captured webElement
Example:
#FindBy(xpath = "//div[#class='modal-footer']//button[text()='Confirm']")
WebElement clickOnConfirmButton;
No such element exception error is displayed.
I am attaching the screenshot of the html as well as the webpage on which I am working.
Note:
When I try to find the element using xpath on the console using $x() of the browser, it gets detected. However when I try to execute my code using the same xpath it throws a no such element exception.
Warning modal is a modal inside a modal.
please check if there is any frame on Dom ,if it is there then just switch to the frame and perform the action.
If frame is not exists the go for css selector along with shycronisation
Solution:1
You need to introduce webDriverWait here i.e visibility of element before performing any action.
Solution:2
You should try to switch to alert and then press on the confirm button.
driver.switchTo().alert().accept();

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 to check if a popup window is open or not using Selenium WebDriver in Java

I'm trying to check whether the popup window I want to open is opened or not.
I have checked some question answers like
How would you check if a popup window exists using selenium webdriver?
But, nothings helped to solve the problem.
Here, first I open the login window by clicking the login button.
driver.findElement(By.xpath("//a[#id='login_btn']")).click(); // Click Login Button
I even tried getPageSource() but, it seems not working.
Any kind of help would be appreciated.
Thanks in advance. :)
If it's a native, browser alert (= a popup) you can do the following:
try{
driver.switchTo().alert();
// If it reaches here, it found a popup
} catch(NoALertPresentException e){}
What it looks like you're actually dealing with is an iframe which you can do the following after getting the attribute value of the "iframe" attribute:
driver.switchTo.frame("ValueOfIframe");
// Treat as normal webpage. Now in iframe scope
driver.switchTo.defaultContent(); // To return back to normal page scope
String mwh=driver.getWindowHandle();
Now try to open the popup window by performing some action:
driver.findElement(By.xpath("")).click();
Set s=driver.getWindowHandles(); //this method will gives you the handles of all opened windows
Iterator ite=s.iterator();
while(ite.hasNext())
{
String popupHandle=ite.next().toString();
if(!popupHandle.contains(mwh))
{
driver.switchTo().window(popupHandle);
/**/here you can perform operation in pop-up window**
//After finished your operation in pop-up just select the main window again
driver.switchTo().window(mwh);
}
}

How to deal with popup messages selenium webdriver - 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();

Categories

Resources