【问题标题】:Selenium getAttribute(href) return null value using java - Salesforce applicationSelenium getAttribute(href) 使用 java - Salesforce 应用程序返回空值
【发布时间】:2019-09-30 07:07:25
【问题描述】:

这是 salesforce 应用程序。 我想从<a> 标记中获取以下属性值

  1. 参考

  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>

Selenium 代码(groovy 脚本语言)

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

注意: 当我在 FireFox 中执行上述代码时,我得到了预期的结果

【问题讨论】:

  • Java 的奇怪语法。
  • 可以分享网址吗?

标签: java selenium salesforce-lightning


【解决方案1】:

要提取hreftitle 属性的值,您必须为visibilityOfElementLocated()elementToBeClickable() 诱导WebDriverWait,您可以使用以下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"));
      

【讨论】:

  • 感谢您的 cmets,但它不起作用。再次获得 null
  • @Prabu 如果这个元素在任何 iframe 中,你能检查一下 HTML 吗?
  • 没有 iframe,当我使用具有相同 XPath 的 getText() 方法时得到了值,但是当我使用 getAttribute() 时得到 null
  • @Prabu 查看更新的答案并告诉我状态。
【解决方案2】:

而不是像//one-app-nav-bar-item-root[${i}]//a[1]这样的xpath

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

【讨论】:

  • 你能用 //a 上的 for 循环打印并查看 DOM 中的链接是否可用吗?
  • 是的,我验证了,所有元素都正确加载到了 DOM 中
猜你喜欢
  • 2021-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-22
  • 2019-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多