0
votes

For complex site that has a step hierarchical structure (Region-Site-Zone-ZoneID), I'm trying to build a dynamic xpath for counting 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>

Code

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);

    }

} 

The problem: How to combine the code line By ByZoneName = By.xpath("//span[.='"+zoneName+"']"); for dynamic zoneName id (for the same execution VAD1, VAD2, VAD3.... VAD10)

Actual: This structure is executed correctly for zoneName=VAD1 and after this in the second curcle is failed with Exception --- Unable to locate element: {"method":"xpath","selector":"//span[.'VAD']"}

Question: How to create dynamic structure for xpath with zoneName?

i.e. By ByZoneName = By.xpath("//span[.='"+zoneName.lastIndexOf(num)+"']"); is failed with Exception Unable to locate element: {"method":"xpath","selector":"//span[.='-1']"}

1
Can you edit your question and clearly show the XML which you are trying to parse with XPath? - Tim Biegeleisen

1 Answers

0
votes

Inside your loop you need to either initially establish the web page or alternatively, navigate BACK to that original page at the end of the loop before attempting to locate and click another web element.

It cannot locate the next zone because the page is not the same.