用于复杂动态范围的java Selenium xpath
对于具有阶梯层次结构的复杂站点(区域站点ZoneID),我尝试构建一个动态xpath来计算ZoneID(1..10)
<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>
代码
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);
}
}
问题:如何组合代码行 按ByZoneName=按。xpath(“//span[.=”+zoneName+“]”) 对于动态zoneName id(对于相同的执行VAD1、VAD2、VAD3…VAD10)
实际值: 对于zoneName=VAD1,此结构正确执行,之后,第二个curcle失败,出现异常 ---找不到元素:{“方法”:“xpath”,“选择器”:“//span[.'VAD']”
问题: 如何使用zoneName为xpath创建动态结构
即 按ByZoneName=按。xpath(“//span[.=”+zoneName.lastIndexOf(num)+“]”); 异常失败 找不到元素:{“方法”:“xpath”,“选择器”:“//span[.='-1']”
# 1 楼答案
在循环中,您需要首先建立web页面,或者在尝试查找并单击其他web元素之前,导航回循环末尾的原始页面
它找不到下一个区域,因为页面不同