Selenium getAttribute(href)使用Java-Salesforce应用程序返回空值

问题描述 投票:0回答:1

这是Salesforce应用程序。我想从<a>标签获得以下属性值

  1. HREF

  2. 标题

HTML代码

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

硒代码(常规脚本语言)

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")
}

输出

null
null
java selenium xpath webdriverwait xpath-1.0
1个回答
0
投票

要提取hreftitle属性的值,必须为visibilityOfElementLocated()引入WebDriverWait,并且可以使用以下Locator Strategies

  • 的xpath

    • href

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

      System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//one-app-nav-bar-item-root/a[//span[text()='Home']]"))).getAttribute("title"));
      
© www.soinside.com 2019 - 2024. All rights reserved.