Selenium webdriver 只有在新窗口中打开时才切换到新窗口,并验证页面是否正确加载

Selenium webdriver 只有在新窗口中打开时才切换到新窗口,并验证页面是否正确加载,selenium-webdriver,Selenium Webdriver,我正在尝试单击下拉列表中的所有链接。某些链接将打开一个新的浏览器窗口,其中某些链接将加载到同一页面中 因此,我只想在新窗口打开时切换到新窗口,还需要确认新页面是否正确加载。 我已经有了切换窗口的代码。但它试图切换新窗口,即使新窗口未打开 有人能帮我吗 下面是相关的HTML片段: <html> <div class="column span-8"> <div class="search-container align-center">

我正在尝试单击下拉列表中的所有链接。某些链接将打开一个新的浏览器窗口,其中某些链接将加载到同一页面中

因此,我只想在新窗口打开时切换到新窗口,还需要确认新页面是否正确加载。 我已经有了切换窗口的代码。但它试图切换新窗口,即使新窗口未打开

有人能帮我吗

下面是相关的HTML片段:

<html>
    <div class="column span-8">
        <div class="search-container align-center">
            <div class="search">
                <div class="search-dropdown" onclick="showHideUtilMenu(event, 'SearchMenu');">
                    <span id="searchoptionValue">Consumer</span>
                    <span id="dropdownarrow"</span>
                    <span id="searchurlValue" style="display: none;">http://</span>
                    <div id="SearchMenu" class="utility-menu combobox visible">
                        <ul class="menu-list">
                            <li class="menu-link">
                                <li class="menu-link divider">
                                    <a onclick="getSearchOption('Consumer','http://');">Consumer</a>
                                ...

消费者

我希望你需要像下面这样的方法

class CommonFunctions {
   /**
   * It will wait for an element until it loads completely
   */
  public static void waitForPageToLoad() {
        int i=0;
    while(isElementPresent(By.id("<specifySomeLocator>")))
    {
        Thread.sleep(100);
        if(++i>500)
        {
            break;
        }
    }
  }

  /**
   * It will Switch the control to new window if exist.
   */
  public static boolean switchToWindow(String mainWindowHandle) {
     Set<String> windowhandles=driver.getWindowhandles();
     if(windowhandles.size()>1) {
        windowhandles.remove(mainWindowHandle);
        driver.switchTo().window((String)windowhandles.toArray()[0]);
        return true;
     } else {
        return false;
     }
  }

}
List<WebElement> list = driver.findElements(By.xpath("//*[@class='search-dropdown']/div[1]/ul/li"));
for (int i=0;i<list.size();i++)
{
    String strLabel = driver.findElements(By.xpath("//*[@class='search-dropdown']/div[1]/ul/li")).get(i).getText();
    if(strlabel.equalsIgnoreCase(value1) && country.equals("US"))
    {
        String mainWindowHandle=driver.getWindowHandle();
        option.findElement(By.linkText(strLabel)).click();
        CommonFunctions.waitForPageToLoad();
        if(CommonFunctions.switchToWindow(mainWindowHandle)){
           //do your operation in new window
           //close new window using ==> driver.close();
           //switch back to main window ==> driver.switchTo().window(mainWindowHandle);
        } else {
           //do your operations in main window itself
        }
    }
}

对于iElementPresent方法实现,请单击

,这是一个一般性建议,而不是对您的问题的直接回答

您可以使用以下任一选项从当前网页导航到另一网页:

driver.get(...);
driver.navigate().to(...);
driver.switchTo().frame(...);
driver.switchTo().window(...);
但是,一旦这样做,您从上一个web页面的DOM中获得的任何元素都可能会变得“过时”

为了避免这种情况,建议您首先获取所有要访问的URL(字符串),然后导航到其中的每一个

以问题中的代码为例,您可以按如下方式更改它:

List<String> strLabels = new ArrayList<String>();
List<WebElement> list = driver.findElements(By.xpath("//*[@class='search-dropdown']/div[1]/ul/li"));
for (WebElement option : list)
{
    String strLabel = option.getText();
    if (strlabel.equalsIgnoreCase(value1) && country.equals("US"))
        strLabels.add(strLabel);
}
for (String strLabel : strLabels)
{
    driver.findElement(By.linkText(strLabel)).click();
    CommonFunctions.waitForPageLoad();
    CommonFunctions.switchWindow();
}
List strLabels=newarraylist();
List List=driver.findElements(By.xpath(“/*[@class='search-dropdown']/div[1]/ul/li”);
用于(WebElement选项:列表)
{
字符串strLabel=option.getText();
if(标准标签同等信号案例(价值1)和国家同等(“美国”))
添加(strLabel);
}
for(字符串strLabel:strLabels)
{
driver.findElement(By.linkText(strLabel)).click();
CommonFunctions.waitForPageLoad();
CommonFunctions.switchWindow();
}

您能否提供您试图控制的网页的URL,或者至少提供相关的HTML片段?消费者http://
  • 消费者代码:代码片段:list list=driver.findElements(By.xpath(“/*[@class='search-dropdown']]/div[1]/ul/li”);对于(WebElement option:list){String strLabel=option.getText();if(strLabel.equalsIgnoreCase(value1)和&country.equals(“US”){option.findelelement(By.linkText(strLabel))。单击();单击();CommonFunctions.waitForPageLoad();CommonFunctions.switchWindow();}
    driver.get(...);
    driver.navigate().to(...);
    driver.switchTo().frame(...);
    driver.switchTo().window(...);
    
    List<String> strLabels = new ArrayList<String>();
    List<WebElement> list = driver.findElements(By.xpath("//*[@class='search-dropdown']/div[1]/ul/li"));
    for (WebElement option : list)
    {
        String strLabel = option.getText();
        if (strlabel.equalsIgnoreCase(value1) && country.equals("US"))
            strLabels.add(strLabel);
    }
    for (String strLabel : strLabels)
    {
        driver.findElement(By.linkText(strLabel)).click();
        CommonFunctions.waitForPageLoad();
        CommonFunctions.switchWindow();
    }