Unable to move focus using Action class in selenium webdriver - selenium

Hello I am working with popup which are made by
So while I click on one button i.e. "click1" it pops up one div window ..
So on this pop up I can play with element by using action class
WebElement element = wd.findElement(By.className("qx-window"));
Actions actions = new Actions(wd);
actions.moveToElement(element).click().perform();
Now from this popup while i click on another button it again pops up another popup and again I tried with action class but unable to set focus on the new pop up
So scenario is main window->popup->popup
can I able to remove focus from first pop up
Hear below selenium code is not working that's why I use action
for (String popup : wd.getWindowHandles())
{
wd.switchTo().window(popup);
}

Try with below flow
//Store main window handle in one variable
String mainWindowHandle = driver.getWindowHandle();
//click something on window to open popup1
Set<String> windowHandles = driver.getWindowHandles(); //It returns Set of available window handles
//from above set get popup1 handle and switch control to popup1
//==switch control to pop-up1
windowHandles.remove(mainWindowHandle);
String popup1Handle=(String)windowHandles.toArray()[0];
driver.switchTo().window(popup1Handle);
//do your operations in popup1
//===== YOUR ACTIONS GOES HERE FOR POPUP1
//click on button in popup1 which will open popup2
//switch the control to popup2
windowHandles = driver.getWindowHandles(); //It returns Set of available window handles, here it returns 3 window handles
windowHandles.remove(mainWindowHandle);
windowHandles.remove(popup1Handle);
String popup2Handle=(String)windowHandles.toArray()[0];
driver.switchTo().window(popup2Handle);
//do your operations in popup2
//===== YOUR ACTIONS GOES HERE FOR POPUP2
driver.close(); // this will close popup2 as control is in popup2
//switch the control to main window again
driver.switchTo().window(mainWindowHandle);

Related

Focus on newly opened tab

I click a login button in my app that causes new tab to be openned but altough Chrome displays content of the new tab ChromeDriver still is operates on the old tab.
Question: How to make ChromeDriver "follow" newly opened tab so I can interact with elements in it?
when you open another tab using selenium, even though it opened another tab it will still operate on the parent tab(the one that is inside/invoked in the get method) in order to operate on the child tab(newly opened tab) you need to do this
//storing all the windows opened by default parent is index 0
Set<String>ids=driver.getWindowHandles();
Iterator<String> it = ids.iterator();
String parentId = it.next(); //1st one = parent tab
String childId = it.next(); //2nd one = child tab
driver.switchTo().window(childId); //switch to child window
System.out.println(driver.getTitle());
driver.switchTo().window(parentId); //switches back to parent window
You just need to switch to the new tab.
By default selenium driver will stay on the first tab that the driver has opened.
Selenium uses these 2 methods to handle it.
driver.getWindowHandle() [It will get the current tab handle]
driver.getWindowHandles() [It will get all the tab handles that are
open]
So, you need to store all the tabs in a variable and handle them one by one.
See the below example.
//I am using a set string "allWindowHandles1" to store all the tabs.
//I am using a simple string "handle1 " to handle the tabs one by one[if present]
//You can use the below for each loop in future if there is multiple child windows also
Set<String> allWindowHandles1 = driver.getWindowHandles();
for(String handle1 : allWindowHandles1) {
Thread.sleep(2000);
driver.switchTo().window(handle1);
//You can write your code to handle the elements in the child window here
//Now the driver will be in your child window
Thread.sleep(2000);
}
String winHandleNew = driver.getWindowHandle();
//Go through loop to get handle of newest window opened
for(String winHandle : driver.getWindowHandles()){
winHandleNew = winHandle;
}
// Switch to newest window opened
driver.switchTo().window(winHandleNew);
You need switch first, use .switchTo().window:
//handle original window
String original = driver.getWindowHandle();
//do something here to bring new window
btn.click();
for(String handle: driver.getWindowHandles()) {
driver.switchTo().window(handle);
if(!driver.getWindowHandle().equals(original)) {
//perform with new window
}
}
//back to original window
driver.switchTo().window(original);
With the above code you can switch again to original window.

Selenium IE11 Alert pop up handle does not work for close browser popup

Issue:
I am using IE11 with Selenium Webdriver, When I try to close the IE browser, I get a "Message From WebPage" popup display, I am trying to click "OK" but Alert handle does not work, It doesn't click "OK".
Selenium version: 3.12.0
IEDriverServer (32bit) version: 3.12.0
public void selectReqFolder() throws Exception {
windowHandle = new WindowsHandle();
//Clicking here open new child window
driver.findElement(Contract_File_Action_Copy_Frwd_Trnsction_button_Solcitation_Link).click();
//Window handle method, switch focus on to child window and does all the task in there
windowHandle.windowsHandle();
//Using window handles to switch to parent window
Set<String> s1 = driver.getWindowHandles();
// Now we will iterate using Iterator to go over the totoal windows
Iterator<String> I1 = s1.iterator();
// List of the windows
String parent = I1.next();
String child_window = I1.next();
**[![// Here we will compare if parent window
driver.switchTo().window(parent);
//Closing the broswer
driver.close();
Thread.sleep(4000);
//Handeling Alert
Alert alert = driver.switchTo().alert();
// Capturing alert message.
String alertMessage= driver.switchTo().alert().getText();
//To click on OK button of the alert
driver.switchTo().alert().accept();][1]][1]**
}

Selenium Webdriver - Accessing 2nd Child window(Popup)

I am automating an application in IE where if user clicks a link on the main window , a child window popups. User further clicks another link from the child window where a 2nd child window popsup. Please find the screenshot of the same application screenshot
Problem is that the 3rd popup window is behind the 2nd popup window. with the following code i am able to get the title for the 3rd popup window, but cannot able to work (Like click on any link etc) over there.
Please find below the code which i have used to navigate to 3rd window from the 2nd.
`String Mw1 = driver.getWindowHandle();
//User clicks a radio button on 2nd window
driver.findElement(By.id("CallType-0")).click();
//User click a submit button and after this the 3rd window popsup
driver.findElement(By.id("cmdLogCall")).click();
Set<String> r1=driver.getWindowHandles();
Iterator<String> i2 =r1.iterator();
while (i2.hasNext())
{
String childwindow2 = i2.next();
if(!Mw1.equalsIgnoreCase(childwindow2))
{
driver.switchTo().window(childwindow2);
String z = driver.getTitle();
System.out.println(z);
driver.findElement(By.id("overridelink")).click();
}
}`
Kindly let me know how can i access the 3rd window.Thanks
Use the following code to switch onto the required window
ArrayList<String> allWindows = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(allWindows.get(2));
System.out.println(driver.getTitle());
driver.switchTo().defaultContent();

selecting checkboxes in a popup window using selenium webdriver

I have a textbox and a popup window opens on click of this textbox. This popup window contains checkboxes.
I want to click on the above textbox and move the focus to the popup window, select the checkboxes in the popup window and move the focus back to main window.
Image of source code of the popup window is attached in the image tab.
Firefox v33.1
Selenium v2.25
source code
I tried with the below code but it didnt work:
driver.findElement(By.id("FieldView_ctl17_MultiSelect1_InputText")).click();
driver.switchTo().activeElement();
driver.findElements(By.id("checkbox0")).click();
You need to switch on open popup window before finding checkbox as below:-
//First store parent window to switch back
String parentWindow = driver.getWindowHandle();
//Perform the click operation that opens new window
driver.findElement(By.id("FieldView_ctl17_MultiSelect1_InputText")).click();
//Switch to new window opened
for(String winHandle : driver.getWindowHandles()){
if(!winHandle.equals(parentWindow)) {
driver.switchTo().window(winHandle);
}
}
//Now find checkbox and click
driver.findElements(By.id("checkbox0")).click();
//Now close opened popup window
driver.close();
//Switch back to parent window
driver.switchTo().window(parentWindow);
//Continue with parent window

How to close a popout window using selenium

I'm very new to Selenium and need to be able to close a popout window. I can get it open, but I can't seem to make the code recognize there's a new window. I've tried every code I've found in all of these questions, with parents, and handles and I'm just more confused. My skills are very basic, but any help would be appreciated. The pop up window that opens has a close button, and I have the name for it to click, but I really need the simplest way for the code to recognize the new window before it gets to the close button.
Any help is appreciated!
Going to try to add the code here.
owd.findElement(By.xpath(vath)).click(); //Click ATH button
System.out.println("ATH Report opened");
//#1
//owd.get(vURL2);
//System.out.println("popout is opened");
//#2
//String parentWindowHandler = owd.getWindowHandle(); // Store your parent window
//String subWindowHandler = null;
//#3
//Select handles = (Select) owd.getWindowHandles(); // get all window handles
//Iterator<String> iterator = ((Set<String>) handles).iterator();
//while (iterator.hasNext()){
// subWindowHandler = iterator.next();
//}
//owd.switchTo().window(subWindowHandler); // switch to popup window
// perform operations on popup
//owd.switchTo().window(parentWindowHandler); // switch back to parent window
//#4
//owd.switchTo()).handle().accept();
//#5
//String winHandle = owd.getWindowHandle(); //Get current window handle.
//for(String windowsHandle : owd.getWindowHandles()) {
// owd.switchTo().window(windowsHandle); //Iterate to the new window handle.
/*
Do any action on window or just close it.
*/
// owd.switchTo().window(winHandle); //Switch to original window.
//#6
owd.switchTo().window(vwindow);
owd.findElement(By.xpath(vclose)).click(); //Click Close button
System.out.println("Close button clicked");
you need to switch to active window/popup alert/frame firstly:
driver.SwitchTo().Window(<windowname>)
driver.SwitchTo().Frame("iFrmLinks")
and to close as allways.
if you are talk about alerts:
driver.SwitchTo().Alert();
alert.accept();