问题
Can anyone help me how to switch to new window after clicking on a hyperlink using selenium while doing automation . I have tried the following code but my test case is failing :
public void openView(){
final WebElement visa=driver.findElement(By.id("pageContainer"));
timeOut(10000);
final List<WebElement> images=visa.findElements(By.className("nametag"));
String handlewindow = driver.getWindowHandle();
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
images.get(0).findElement(By.className("info")).findElement(By.tagName("a")).click();;
回答1:
Assuming that the last line of code:
images.get(0).findElement(By.className("info")).findElement(By.tagName("a")).click()
opens a new window, you are fetching the window handle of the parent window itself before the child window opens after click().
Secondly, here is the code for switching to a child window:
String handlewindow = driver.getWindowHandles().toArray()[1];
driver.switchTo().window(handlewindow);
The explanation is that driver.getWindowHandles() returns a Set of all open windows handles. Convert the Set to array and fetch the element at index 1 as child window is at 1, parent window is at 0.
回答2:
This what we're using (Java):
public void switchToLatestPopupWindow()
{
Iterator<String> handleIterator = this.driver.getWindowHandles().iterator();
while (handleIterator.hasNext())
{
String handle = handleIterator.next();
if (!handleIterator.hasNext())
{
this.driver.switchTo().window(handle);
}
}
}
public void resetToMainWindow()
{
Iterator<String> handleIterator = this.driver.getWindowHandles().iterator();
String parentHandle = null;
while (handleIterator.hasNext())
{
String handle = handleIterator.next();
if (parentHandle == null)
{
parentHandle = handle;
} else if (!parentHandle.equals(handle))
{
this.driver.switchTo().window(handle);
this.driver.close();
}
}
this.driver.switchTo().window(parentHandle);
}
回答3:
Use this code picked up from another thread on Stackoverflow ...
WebElement link = driver.findElement(By.linkText("<your link text>"));
Actions newwin = new Actions(driver);
newwin.keyDown(Keys.CONTROL).click(link).keyUp(Keys.CONTROL).build().perform();
I checked this and it opens the link in a new tab. Then use this code to switch to the new tab ...
String handlewindow = driver.getWindowHandles().toArray()[1];
driver.switchTo().window(handlewindow);
回答4:
In the for loop we need to check if the current window handle is not equal to the one which is fetched by the loop
String handlewindow = driver.getWindowHandle();
for(String winHandle : driver.getWindowHandles()){
if (! handlewindow.equals (winHandle))
driver.switchTo().window(winHandle);
}
回答5:
Actions ac = Actions(driverObj);
String parentHandle = driverObj.getWindowHandle();
WebElement we = driverObj.findElement(By.xpath("here is your xpath"));
ac.keyDown(Keys.CONTROL).click(we).keyUp(Keys.CONTROL).build().perform();
String currentHandle ="";
Set<String> win = ts.getDriver().getWindowHandles();
Iterator<String> it = win.iterator();
if(win.size() > 1){
while(it.hasNext()){
String handle = it.next();
if (!handle.equalsIgnoreCase(parentHandle)){
ts.getDriver().switchTo().window(handle);
currentHandle = handle;
}
}
}
else{
System.out.println("Unable to switch");
}
Using above code you can open any link in new tab..
回答6:
For switching between tabs you can use this methods:
public void switchToNextTab() {
ArrayList<String> tab = new ArrayList<>(driver.getWindowHandles());
driver.switchTo().window(tab.get(1));
}
public void switchToPreviousTab() {
ArrayList<String> tab = new ArrayList<>(driver.getWindowHandles());
driver.switchTo().window(tab.get(0));
}
来源:https://stackoverflow.com/questions/39409268/switch-to-new-window-after-clicking-on-the-hyperlink-using-selenium