【问题标题】:Selenium xpath for complex dynamic span用于复杂动态跨度的 Selenium xpath
【发布时间】:2017-12-19 00:58:19
【问题描述】:

对于具有阶梯层次结构 (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>

代码

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 = By.xpath("//span[.='"+zoneName+"']"); 对于动态zoneName id(对于相同的执行VAD1、VAD2、VAD3.... VAD10)

实际: 此结构为 zoneName=VAD1 正确执行,之后在第二个 curcle 中失败并出现异常 --- 无法定位元素:{"method":"xpath","selector":"//span[.'VAD']"}

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

By ByZoneName = By.xpath("//span[.='"+zoneName.lastIndexOf(num)+"']"); 因异常而失败 无法定位元素:{"method":"xpath","selector":"//span[.='-1']"}

【问题讨论】:

  • 您能否编辑您的问题并清楚地显示您尝试使用 XPath 解析的 XML?

标签: java xpath webdriver


【解决方案1】:

在您的循环中,您需要先建立网页,或者在循环结束时导航回原始页面,然后再尝试定位并单击另一个网页元素。

由于页面不一样,无法定位下一个区域。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-14
    • 2021-07-30
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    • 2014-07-27
    • 2018-09-22
    • 2017-01-29
    相关资源
    最近更新 更多