Unable to switch to a dialog box in Selenium-WebDriver using Java? - java

When I clicked on a button a new dialog box appeared in the same browser. Is it possible to switch to the dialog box in Selenium-WebDriver using Java?
I found that it's not an alert or there is no iframe present in the page.
As soon as the dialog box opens the parent window page is loading until I close the box and the new window appears in the same browser.
I tried window handles option but none of them solved my issue.
Please suggest a solution to this?

if the window object is identifiable using AutoIT spy tool, then you can simply use AutoIT java wrapper classes. The code will look something like this,
WindowObject = ApplicationObject.FindWindow("Your window title goes here");
WindowObject.ControlClick("Windowtitle","",ControlID);

If it is a Modal Dialog Box, you can do something like:
driver.switchTo().alert();
alert.accept(); // TO ACCEPT THE ALERT
OR
alert().dismiss(); // TO DISMISS IT WITHOUT ACCEPTING

Related

Unable to click on this Button using selenium (java)

i have issues with click(); function using selenium webdriver.
here's the outputHtml :
<button type="button" class="k-button k-primary" tabindex="0" style="width: 50%;">Valider< /button>
i used the findEelement using various xpath like :
- "//button[#class = 'k-button k-primary' and #type = 'button']"
- "//*[contains(#class, 'k-button k-primary') and text()='Valider']"
and still got the error Unable to locate the element
here's the screenshot of the pop-up's Button that i need to click on (button Valider)
Button : Valider
Have you tried actually copying and pasting the selector?
If you are on a Windows machine:
In Google Chrome if you press F12 to open the developer window.
Find the node you are looking for within the DOM and right click on it.
Then go to "Copy" and "Copy XPath" or you can "Copy JS Path"
Then you can paste the path into your code and try it that way.
I prefer to select dom nodes using the JS Path when I am developing with selenium.
The "Copy Full XPath" option is useful during development if you are having problems like you are because it copies the full path all the from the root node.
This feature is available in all major browsers but I am most familiar with developing with Google Chrome.
Are you sure the Element has loaded when you are searching for it? Try a break point before and verify it is available.
In order to handle alerts/pop-ups you need to switch to it
//switch to alert
Alert alert = driver.switchTo().alert();
//accept it
alert.accept();
// or accept it by your code
driver.findElement(By.XPATH("//button[#class = 'k-button k-primary' and #type = 'button']").click();
//dismiss alert
alert.dismiss();
//or dismiss with you code
driver.findElement(YOUR_LOCATOR).click();
//get text
String alertText = alert.getText()
Please check if the pop up is within some frame. If it is then you have to switch to the frame like this
driver.switchTo.frame(iframe locator)

Handling custom pop-ups (not the default windows one) through selenium webdriver (3.x)

I want to automate a scenario.
When the browser lands on the website, there is a warning pop-up that requires a response to the prompt: Do you want to proceed?
There are two options Leave and Continue.
I am trying to switch the control with the following functions but its not working.
Alert alert=driver.switchTo().alert();
driver.switchTo().alert();
alert.accept();
If its JavaScript alert, then driver.switchTo().alert().accept(); should work by accepting the default.
Is this pop-up or modal window?
Did you try to switch to window and click on the button?
Also, which browser are you using? JavaScript alert might needs to handled differently based on the browser.
If its modal or window, then getWindowHandle() should work fine.
String newWindow = driver.getWindowHandle();
driver.switchTo().window(newWindow);
//Switching to new window
driver.findElement(By.id("buttonId"));
//Switching back to default/main window
driver.switchTo().defaultContent();

Alert Handling in Selenium Webdriver

I tried to switch to popup alert and click on OK button, but i got an error saying that xpath (for OK button) is not found.
But this is working for me sometimes using the same code. Could anyone help me out on this. I tried all possible ways that is available in blogs. But i couldn't make it
you need to move control to your pop-up window first before doing any operation on pop-up window:-
below code is to move selenium control on pop-up window
driver.switchTo().alert();
by writing below line
alert.accept();
alert will get close
Based on the original question and the subsequent comments, I suspect what you're dealing with is a browser pop up window and not an alert. So this won't work
driver.switchTo().alert().accept();
You need to use window handles
Set<String> handles = driver.getWindowHandles();
Iterator<String> windows = handles.iterator();
String parent = windows.next();
String child = windows.next();
driver.switchTo().window(child);
driver.findElement(By.xpath("insert xpath to OK button")).click();
driver.switchTo().window(parent);
//continue with steps on parent window
Note: ensure that you add the required synchronization to the code snippet above

How to get object of Pop-up screen that blocks the main screen using firepath in selenium webdriver?

I am writing an automation script in Java in selenium webdriver. When I try to logoff my main window screen I am getting a pop-up window that does not allow me to go to the main screen unless I click some option "Leave page" or "Stay on page" . Can I get the script to close this screen and logoff the main application.Firepath does not work for this as well. I am a beginner trying to learn automation.Thanks.
Adding some of your code to the question would be useful...
Here is a wild guess, assuming that you have a WebDriver driver instance:
driver.switchTo().alert().accept(); // Leave Page
driver.switchTo().alert().dismiss(); // Stay on page
Try these first:
driver.switchTo().alert().accept(); // to leave page
or
driver.switchTo().alert().dismiss(); // to accept page
if doesn't work, then you can use Robot class to press Enter or any Key like -
Robot robot = new Robot();
robot.setAutoDelay(3000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Only thing you've to notice - which button the default cursor or which one is highlighted, Robot will perform action on the highlighted object only, from there you can press tab to go anywhere else.
Hope this helps.

I need to read the text displayed on the popup window in Web driver using Java

I need to read the text displayed on the popup window in Web driver using Java. I am able to handle the popup window for closing. I don't know how to read the text displayed on Popup window and print it in Console.
I am not able to provide any HTML code for this because its a Modal Popup window.
Please help me on this. Help will be appreciated.
Given your screenshot, it looks like the "modal popup" you're trying to automate is generated by the JavaScript alert() function. If this is the case, the following code or something similar to it, should work.
// WARNING! Untested code written from memory without
// benefit of an IDE! May not be exactly correct!
// Switch the driver context to the alert
Alert alertDialog = driver.switchTo().alert();
// Get the alert text
String alertText = alertDialog.getText();
// Click the OK button on the alert.
alertDialog.accept();
Have you used the WebDriverWait object before? To expand on the previous answer, you may be able to do something similar to this, but I have not tested:
WebDriverWait wait = new WebDriverWait(5, TimeUnit.Seconds);
element.click();
// Wait for the dialog to show
wait.until(ExpectedConditions.alertIsPresent());
// Switch the driver context to the alert
Alert alertDialog = driver.switchTo().alert();
// Get the alert text
String alertText = alertDialog.getText();
// Click the OK button on the alert.
alertDialog.accept();
Also, you may have to switch back to the alert after getting the text. Hope this helps.

Categories