Selenium xpath用于复杂的动态跨度

时间:2017-12-16 10:50:09

标签: java xpath webdriver

对于具有步骤层次结构(Region-Site-Zone-ZoneID)的复杂站点,我试图构建一个动态xpath来计算ZoneID(1..10)

Structure



<div class="aic-tree-branch-content VAD2" ng-click="events.selectZone(site.id, zone.id)">
  <span class="new-sprite unimported selected" ng-class="{'imported': zone.zoneImported, 'unimported': !zone.zoneImported, 'selected': zone.zoneName === selecteds.zone}"></span>
  <span class="aic-tree-branch-content-name ng-binding" ng-bind-html="zone.zoneName | highlightFilter: model.searchTerm" ng-click="ui.selectTreeNode(zone.zoneName, 'zone')">VAD2</span>
  <span class="aic-tree-branch-content-type zone-type ng-binding" ng-bind="'('+zone.designTypeName+')'">(M)</span>
  <span class="aic-tree-branch-content-icon new-sprite zone-state in-creation-small" ng-class="ui.getZoneStatusIcon(zone.zoneState, zone.zonePhase)"></span>
</div>
&#13;
&#13;
&#13;

代码

&#13;
&#13;
public static void refreshAndOpenMultiZones(WebDriver driver, String SiteName, String zoneName) throws Exception {

    driver.navigate().refresh();

    for (int num=1; num<3; num++) {

        logger.info("Open existing zone: " + SiteName + num + " in North America");

        //Select desired zone in site
        By ByZoneName = By.xpath("//span[.='"+zoneName+"']");
        
        logger.info("Select Zone: "+ zoneName);
        Utils.wait(5);
        driver.findElement(ByZoneName).click();

        logger.info("Wait for page to be loaded");
        GeneralUtils.waitForElevationPage(driver, timeOutSec);

    }

} 
&#13;
&#13;
&#13;

问题:如何组合代码行        By ByZoneName = By.xpath(&#34; // span [。=&#39;&#34; + zoneName +&#34;&#39;]&#34;); for dynamic zoneName id(对于相同的执行VAD1,VAD2,VAD3 .... VAD10)

实际: 对于zoneName = VAD1,此结构正确执行,在此之后,第二个curcle中的结构因Exception而失败 ---无法定位元素:{&#34;方法&#34;:&#34; xpath&#34;,&#34;选择器&#34;:&#34; // span [。&#39; VAD& #39;]&#34;}

问题: 如何使用zoneName创建xpath的动态结构?

By ByZoneName = By.xpath(&#34; // span [。=&#39;&#34; + zoneName。 lastIndexOf(num) +&#34;&#39;]& #34); 因异常而失败 无法找到元素:{&#34;方法&#34;:&#34; xpath&#34;,&#34;选择器&#34;:&#34; // span [。=&#39; -1&# 39;]&#34;}

1 个答案:

答案 0 :(得分:0)

在循环内部,您需要首先建立网页,或者在尝试查找并单击其他网页元素之前,在循环结束时导航回原始页面。

找不到下一个区域,因为页面不一样。