![](/img/trans.png)
[英]Element.getattribute(“Value”) and gettext() is not working for selenium webdriver using Java
[英]Selenium getAttribute(href) return null value using java - Salesforce application
这是 salesforce 应用程序。 我想从<a>
标签中获取以下属性值
参考
标题
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")
}
output
null
null
注意:当我在 FireFox 中执行上述代码时,我得到了我的预期结果
要提取href
和title
属性的值,您必须为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"));
而不是 xpath 像//one-app-nav-bar-item-root[${i}]//a[1]
试试像(//*[@role='listitem'])[${i}]//a[1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.