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);
Related
How to change browser instance in Selenium- Java? WebDriver could not locate the elements on the screen after clicking on second webpage
// First store the current window instance
String winHandleBefore = driver.getWindowHandle();
// After your click operation which opens the new window
// Below code Switches instance to the new window
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
// If you want to switch back to the original window
driver.switchTo().window(winHandleBefore);
I have a page in which I click on a link which opens a new Modal window which has an iframe. I switched to the iframe and performed some validation, then click on the link in that Modal window which in turn opens a second new Modal window with an iframe. I am facing issue clicking on any element in that second new Modal window.
Here is my code.
WebElement Hotelname = driver.findElement(By.cssSelector(".hotelTitleZone2>a"));
Hotelname.click(); \\This will open a new Pop up.
driver.switchTo().frame(1);
\\perform some validation
String parentHandle = driver.getWindowHandle();
driver.findElement(By.linkText("View on a Map")).click(); \\This will open second pop up Modal window
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
}
driver.switchTo().defaultContent();
driver.switchTo().frame(1); \\switching to frame
driver.findElement(By.linkText("Close")).click();
When I am running this code, I am getting an error:
org.openqa.selenium.NoSuchElementException: Unable to locate element:
{"method":"link text","selector":"Close"}
I tried with or without switching to default content, without switching to frame in second Modal window but result is always the same.
Any help is appreciated ? Thanks.
My understanding is:
start from the default window
click to open the first Modal window that has an iframe
Switch to this new iframe (index = 1)
Get the ID for the current window handle, which is the default window handle
click to open the second Modal window that has a second iframe
switch to the second Modal window
switch back to the default window
switch to iframe (index = 1)
Find the button you are after
There are a few confusions here:
in step 4 above, you used String parentHandle = driver.getWindowHandle(); to store the original default window handle but you have never used it, instead, you use driver.switchTo().defaultContent();
What happened to the first Modal window after you clicked it? Did it close? if it had not closed, its iframe would still be iframe (index=1), this would explain why you could not find your button from the iframe (index=1); as your button would reside on the iframe that belongs to the second Modal window, which is likely to be iframe (index=2). You may use driver.switchTo().frame(2); to address it instead. To be sure, you can inspect HTML elements to see how many iframes are present and to which Modal windows they belong to.
Hope you will find it useful.
I need to open a link in a new window with Selenium. I found the following 2 (assume we is the WebElement and I have already found it and myWait just performs a wait for x milliseconds):
1)
Actions act = new Actions(driver);
act.contextClick(we).perform();
myWait(1000); // allow the menu to come up
act.sendKeys(which).perform();
2)
we.sendKeys(Keys.CONTROL + "t");
In #1 I see the menu come up, but the sendKeys does not work (oh, by the way the "which" in sendKeys is a "t"). In #2 it simply ignores it.
In #1 is the menu that comes up in a new window already? Do I have to driver.switchTo()? or if not, what am I doing wrong?
Also, is there another way to do this? we is an element of form <a href=blah> and a regular click opens fine.
You can try the below code to open a link in a new window:
Actions newTab= new Actions(driver);
WebElement link = driver.findElement(By.xpath("//xpath of the element"));
//Opening the link in new window
newTab.contextClick(link).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
Note: After you've opened the link a new window, to get control of it, you need to get the handle of this child window, switch to it using driver.switchTo.window("childwindowhandle") and take further actions. And, after that you can close it and switch back to the Parent window(original one) and take actions here (Make sure to get the parent window handle before you open the link in a new window, so that switching back to it is easier).
I'm using Selenium2 (Webdriver).
Well, I have a Problem.
I have a A Browser.
When I click one button in A Browser, Another popup browser(B) is open.
And, The process what I have to do in B browser is done, B browser wasn't close.
But, this is not my problem. B browser is originally Designed this way.
my problem is ..
After B browser's process is finished, when I tried to find out C element in A browser, So B browser wans't closeed, that I can't find out C element. because Selenium tried to find out C element in B browser.
I just want...
When I do something in Browser B, Browser B has a Handle.
and the Process in Browser is done, the handle must moving in A Browser.
How can I? Please Help me.
Here is the sample code for switching between the Windows:
With windows handle we can switch between windows.
WebDriver driver=new FirefoxDriver();
//First navigating to Yahoo site
driver.get("http://www.Yahoo.com");
//Capturing the window handle
String strMainWindowHandle=driver.getWindowHandle();
//For better understanding printing the page title
System.out.println(driver.getTitle());
//Now opening a new window
driver.findElement(By.xpath("//body")).sendKeys(Keys.CONTROL+"n");
Set<String> winHandles=driver.getWindowHandles();
for(String handle:winHandles)
driver.switchTo().window(handle);
//Navigating to Google site with new window i.e new window handle
driver.get("http://www.google.com");
//For better understanding printing the page title
System.out.println(driver.getTitle());
//Switching back control to main Window which captured earlier
driver.switchTo().window(strMainWindowHandle);
System.out.println(driver.getTitle());
Once you come back to your main window handle you can perform Click or whatever u want.
I have the login page, when click login its opens the new tab. I moved the control to new tab using
driver.switchTo().window("");
// Done some stuffs in new window.
when I click one Button it will open the popup (frame).
I have selected a popup window using driver.switchTo().frame("frameName");
and from there by selecting the record, the popup window will be closed. The selected record will be displayed in parent window and the page got refreshed.
Now I want to return(Re-Focus) the control to my parent window for doing the some other stuffs.
but I could not focus the parent window again.
I have tried:
driver.switchTo().defaultcontent();
driver.switchTo().window("");
and
driver.getWindowHandles();
Still the same result....
Could anyone please help me on this....
String oldWindow = driver.getWindowHandle();
driver.findElement(By.xpath("//a[#id='searchSalesImg']/img")).click();
//by clicking on this we will get a new window
Thread.sleep(5000);
driver.switchTo().window(driver.getWindowHandle());
driver.switchTo().window("Employer Services Order Management (ESOM)");
//Navigate to new window
driver.findElement(By.id(" Primary Sales Person:")).sendKeys("MAS%");
//Do some stuff in new window
driver.switchTo().window(oldWindow);
//navigate back to old window
Try using this code:-
driver.switchTo().frame("Frame_identifier");
//your code
String winHandleBefore = driver.getWindowHandle();
webDriver.SwitchTo().Window(winHandleBefore);