只要确保您切换到正确的窗口
原因1:只要确保您切换到正确的窗口
我有一个实用方法来切换到所需的窗口,如下所示
public class Utility
{
public static WebDriver getHandleToWindow(String title){
//parentWindowHandle = WebDriverInitialize.getDriver().getWindowHandle(); // save the current window handle.
WebDriver popup = null;
Set<String> windowIterator = WebDriverInitialize.getDriver().getWindowHandles();
System.err.println("No of windows : " + windowIterator.size());
for (String s : windowIterator) {
String windowHandle = s;
popup = WebDriverInitialize.getDriver().switchTo().window(windowHandle);
System.out.println("Window Title : " + popup.getTitle());
System.out.println("Window Url : " + popup.getCurrentUrl());
if (popup.getTitle().equals(title) ){
System.out.println("Selected Window Title : " + popup.getTitle());
return popup;
}
}
System.out.println("Window Title :" + popup.getTitle());
System.out.println();
return popup;
}
}
一旦窗口的标题作为参数传递,它将带您到所需的窗口。在你的情况下,你可以做到。
Webdriver childDriver = Utility.getHandleToWindow("titleOfChildWindow");
然后再次使用相同的方法切换到父窗口
Webdriver parentDriver = Utility.getHandleToWindow("titleOfParentWindow");
此方法在处理多个窗口时有效
原因2:等待元素
WebdriverWait wait = new WebdriverWait(driver,7000);
wait.until(ExpectedConditions.visbilityOfElementLocatedBy(By.name("nameofElement")));
原因3:检查元素是否在框架中,如果是则切换到之前的框架
driver.switchTo.frame("frameName");
让知道它是否有效..