How to close pop up window, when there is no XPath for close button? - selenium

When I clicked on a some link, the popup window is opened on new browser.
I want to close that pop up window, but I am unable to find out the locator.
How to close window, when it is opened as popup in Selenium?

Use this this,it worked for me
// Before new window
String currentWindow=driver.getWindowHandle();
//After window new open
Set<String> handles=driver.getWindowHandles();
//to make set having only one single/current window value
handles.remove(currentWindow);
String[] handlesArray = handles.toArray(new String[handles.size()]);
driver.switchTo().window(handlesArray[0]).close();

Related

How to close popup windows in Selenium?

I'm doing a test with java and popup windows appear (ads) and I do not know how to close them to continue with my test.
I have to click on a button but I change the focus to the window.
The url is www.orbitz.com
Please help!
orbitz
Java solution:
//switch to opened tab
ArrayList<String> tabs_windows = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs_windows.get(1));
//close current tab and switch driver back to original
((JavascriptExecutor)driver).executeScript("close();");
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
}
I haven't tested this for a while, but in different browsers getWindowHandles() used to return different orders for the tabs. Not sure if you can assume 0 is the first tab for all browsers. Change the index accordingly, or store the current handle before popup, and close all that don't match that.

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();

Close download bar

I am using Java and Selenium to write a test. Somewhere in my test, I download a file but then need to click on a button under the download bar which appears at the bottom of the chrome browser page. I search a lot but the only solution was here which is not my case as I don't have a scroll.
I also use:
action.sendKeys(Keys.CONTROL+ "j").build().perform();
action.keyUp(Keys.CONTROL).build().perform();
Thread.sleep(500);
ArrayList<String> tabs2 = new ArrayList<String> (driverChrome.getWindowHandles());
driverChrome.switchTo().window(tabs2.get(1));
Thread.sleep(500);
driverChrome.close();
driverChrome.switchTo().window(tabs2.get(0));
Thread.sleep(500);
but it doesn't open the download page.
Anyway that I can close the download bar?
This method did not work for me either, but I developed a workaround. I do any download test in a new window, then close the download window, the original window does not have the download bar. It must be a new window, if you do a new tab it will transfer over, to get this I use JavaScript. Switch to the new window, run download test and then switch to the original window when done.
string javascript = $"$(window.open('', '_blank', 'location=yes'))";
((IJavaScriptExecutor)Driver).ExecuteScript(javascript); //create new window
Driver.SwitchTo().Window(Driver.WindowHandles.Last())); //switch to new window
//do download test here
Driver.Close(); //close created window
Driver.SwitchTo().Window(Driver.WindowHandles.First()); //back to original window with no download bar

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

Close a Window in MSCRM 2013 by Selenium Webdriver

MY Test case is like on click of one Button(Update Customer) a new Window will open,after opening user verifies Data and will CLose the Window. The control need to back to Previous Window
I Have Written driver.close() . but it's not working
I think you problem is that the WebDriver is not focusing on the previous window.
Save your previous window handle:
String myWindowHandle = driver.getWindowHandle();
You can do this after you close the window:
driver.SwitchTo().window(myWindowHandle);
Update
String myWindowHandle = driver.getWindowHandle();
// click operation that opens new window
// Switch to new window opened
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
// Do stuff
driver.close(); // This will close the current window (new one)
// Switch back to first window
driver.switchTo().window(myWindowHandle);