Selenium Web Driver multiple windows - java

Scenario is
IN gmail email popup out if i am clicking on send button Email will send that popup will close automatically once i click on send button
My question is
if child window close automatically after how to switch to parent window?

You can do this by getting a handle to the parent window before you switch to the new window. Then after the new window closes you can switch back to the parent using this handle.
// before opening the new window
String parentWindowHandle = driver.getWindowHandle();
// after closing the new window
driver.switchTo().window(parentWindowHandle);

Related

Selenium WebDriver code cannot track elements in the window, having no Title or URL

I am on a page. After clicking on a link, a new window is opening. The window does not have any URL or title. So, while switching to the new window, aftre I am inspecting element, &, creating Xpath, Selenium code can't focus on the new window, and is not executing anything after that. Can you please resolve my query?
Note: After I inspect, the console window title is showing the URL of the parent window.
You can switch between different open windows you have opened:
String storeCurrentWindowHandler = driver.getWindowHandle();
Then perform the click operation that opens a new window
Make a switch to the new window that has opened
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
Do whatever the test needs within the selected window
Then close the new window, if it is no longer required
driver.close();
And switch back to the first window that had been opened
driver.switchTo().window(storeCurrentWindowHandler);
Hopefully this will help you to solve your issue.

Handling second tab that opens on some operations in first tab in google chrome in java selenium

I have test case to automate in which it will open the gmail and find a received mail using subject and in the body of the mail i have to submit comment in which it will take to the second tab in same window. And i have click on one button in the newly opened tab.
I have tried using the getWindowHandle() and getWindowHandles(). But it has only one window handler for the entire window. Using that handler i can't access the second tab.
String parentHandle = driver.getWindowHandle();
System.out.println(parentHandle);
driver.findElement(By.xpath("//*[#name='comment']")).sendKeys("Comment and Approved From admin Mail");
driver.findElement(By.xpath("//button[contains(text(),'Submit')]")).click();
driver.switchTo().alert().accept();
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
System.out.println(tabs);
tabs.remove(parentHandle);
driver.switchTo().window(tabs.get(0));

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

How to handle new tab vs new window in selenium webdriver in multiple browsers [duplicate]

I need to switch between the browser tabs, used the following code,
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");
It was working properly sometimes only, but sometimes it is showing an exception.
Can anyone suggest me is there any other instructions for switching tabs within a single window by using java.
You have to use window handle function here. Here is a sample working code in java:
String parentHandle = driver.getWindowHandle(); // get the current window handle
System.out.println(parentHandle); //Prints the parent window handle
String anchorURL = anchor.getAttribute("href"); //Assuming u are clicking on a link which opens a new browser window
anchor.click(); //Clicking on this window
for (String winHandle : driver.getWindowHandles()) { //Gets the new window handle
System.out.println(winHandle);
driver.switchTo().window(winHandle); // switch focus of WebDriver to the next found window handle (that's your newly opened window)
}
//Now your driver works on the current new handle
//Do some work here.....
//Time to go back to parent window
driver.close(); // close newly opened window when done with it
driver.switchTo().window(parentHandle); // switch back to the original window
Hope this helps!
Switching between browser window is different from switching b/w tabs.
In some browser windowhandler command may work but it wont work in all browser.
Here is the solution to navigate b/w tabs
for navigating left to right side:
Actions action= new Actions(driver);
action.keyDown(Keys.CONTROL).sendKeys(Keys.TAB).build().perform();
For navigating right to left :
Actions action= new Actions(driver);
action.keyDown(Keys.CONTROL).keyDown(Keys.SHIFT).sendKeys(Keys.TAB).build().perform();
In my case, the following code is working fine-
String oldTab=driver.getWindowHandle();
driver.findElement(pageObj.getL_Popup_Window()).click();
ArrayList<String> newTab = new ArrayList<String>(driver.getWindowHandles());
newTab.remove(oldTab);
driver.switchTo().window(newTab.get(0));
WebElement ele = driver.findElement(pageObj.getI_input_name());
ele.click();
ele.sendKeys(name);
driver.findElement(pageObj.getI_submit()).click();
driver.switchTo().window(oldTab);

Handling dynamically changing element id - using selenium webdriver

I m having the login page, when click login button it opens the new tab.
i moved the control to new window using,
driver.switchTo().window("_blank");
When I click one Button it will open the new popup (that popup is not a normal window it is a iframe).
I have selected a popup window using,
driver.switchTo().frame("frameName");
That popup has the list of records; each record has the separate "select" option (with the dynamically changing id's).
driver.findElement(By.xpath("//a[#id='radgrdPeople_ctl00_ctl04_lnkSelect']")).click();
When selecting the record from list, the popup window will be closed and the page will get refreshed.
Now I want to return the control to my parent window for doing some other stuff.
The record got selected successfully. But i could not able to focus the parent window again.
I have tried:
driver.switchTo().defaultcontent();
driver.switchTo().window("_blank");
And
driver.getWindowHandles()
Still I m getting the same problem.
I don't know whether I have to use any java script executor for handling the dynamically changing element id's.
Could u any one please help me on this....
Thanks in Advance.
By
K.Ranjithkumar
In the post after clicking the login button you used WindowName to switch over to the new window. But, in the tried solution, you used empty string to switch over to the new window.
You should make first window as parent window
String parent=driver.getwindowhandle;
// then, set String popup to be equal to window handle of the
// popup window
driver.switchTo().window( popup);
driver.findElement(By.id("okbutton")).click(); //assumes close of popup
Now after performing tasks on popup window, return control to parent window:
driver.switchTo().window( parent );// switch back to parent window
driver.switchTo().defaultcontent(); // reset iframe context

Categories