How to close popup windows in Selenium? - 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.

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 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

Selenium IDE window focus in Internet Explorer

I'm using selenium IDE and webdriver to test a web page in Internet Explorer. I discovered a while back that IE will not fully accept commands from Selenium if it's not the window in focus. For example, if the Selenium IDE window is in focus, and the command is to click a button in IE, the button will push down, but it won't let go.
With that in mind, my test involves popping up a window, doing a few things in it, leaving it open and returning to the null window to do a few things, then returning to the popup for a few more commands.
Is there a way I can make the null window come forward (over the popup) when I need to execute the commands for the null window? And then vice versa, can I make the popup then come forward when I need to return to it? I tried using windowFocus, but that did not work.
Use the SwitchTo() method and the TargetLocator Interface in Selenium.
A really simple example would look like this:
// Switch to new window
public String SwitchToNewWindow()
{
// Get the original window handle
String winHandleBefore = driver.getWindowHandle();
foreach(String winHandle in driver.getWindowHandles())
{
driver.switchTo().window(winHandle);
}
return Constants.KEYWORD_PASS;
}
// Switch back to original window
public String switchwindowback()
{
String winHandleBefore = driver.getWindowHandle();
driver.close();
//Switch back to original browser (first window)
driver.switchTo().window(winHandleBefore);
//continue with original browser (first window)
return Constants.KEYWORD_PASS;
}
I remembered that webdriver sometimes acts differently than running non-webdriver tests. It turns out that using windowSelect followed by windowFocus switches between windows when running webdriver tests.

How to click on facebook longin dialong window using Selenium tool

Hi All,
I'm new buddy to the selenium web automation tool. I'm stuck with an scenario were in I need to send log in credentials in the Facebook login window page to complete my test case. So please help out or provide some suggestion to pass the credentials and login to the Facebook account.
I tried out using the Selenium IDE and record my test script but while running the recorded script I'm unable to get access to that particular dialog window.
Below I had attached my scenario image
Thanks in Advance.
After lot of trails, I was able to achieved the above scenario by using the following below code.
String winHandleBefore = driver.getWindowHandle();
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
try with this one please
//Store the current window handle
String winHandleBefore = driver.getWindowHandle();
//Perform the click operation that opens new window
//switch to new window opened
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
//Perform the actions on new window
//close the new window, if that window no more required
driver.close();
//Swithc back to original browser (first window)
driver.switchTo().window(winHandleBefore);
//continue with original browser (first window)