0

This is the salesforce application. I want to get the below attributes value from the <a> tag

  1. Href

  2. Title

HTML Code

<one-app-nav-bar-item-root one-appnavbar_appnavbar="" data-id="home" data-assistive-id="operationId" aria-hidden="false" draggable="true" class="navItem slds-context-bar__item slds-shrink-none slds-is-active" role="listitem" xpath="1">
<a href="/lightning/page/home" title="Home" tabindex="0" draggable="false" aria-describedby="operationId-14" class="slds-context-bar__label-action dndItem" style="">
<span class="slds-truncate">Home</span>
</a></one-app-nav-bar-item-root>

Selenium code (groovy scripting language)

for(int i:(1..size)){
            WebElement getHref = driver.findElement(By.xpath("//one-app-nav-bar-item-root[${i}]//a[1]"))
            println getHref.getAttribute("href")
            println getHref.getAttribute("title")
}

output

null
null

NOTE: When I execute the above code in FireFox, I'm getting my expected result

Prabu
  • 3,550
  • 9
  • 44
  • 85

2 Answers2

0

To extract the value of href and title attributes you have to induce WebDriverWait for the visibilityOfElementLocated() or elementToBeClickable() and you can use the following Locator Strategies:

  • xpath:

    • href:

      System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//one-app-nav-bar-item-root/a[//span[text()='Home']]"))).getAttribute("href"));
      
    • title:

      System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//one-app-nav-bar-item-root/a[//span[text()='Home']]"))).getAttribute("title"));
      
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

Instead of xpath like //one-app-nav-bar-item-root[${i}]//a[1]

Try like (//*[@role='listitem'])[${i}]//a[1]

Naveen
  • 770
  • 10
  • 22