2016-10-27 29 views
1

HTML:的getText()澄清 - 的XPath沒有返回值

<strong class="text-xl ng-binding" ng-bind="summary.PublishedIssuedRecent">5</strong> 

我的XPath:

@FindBy(how = How.XPATH, using = "//strong[@ng-bind='summary.PublishedIssuedRecent']") 
public WebElement countRecentlyPublishedApplication; 

String pubCount = countRecentlyPublishedApplication.getText(); 

我的意料

我想提取的5

值但我在pubCount字符串中獲得空值,請建議是否有任何錯誤我的xpath

+0

我假設你使用硒,對不對?如果是這樣,請添加相應的標籤。 – Markus

回答

1

從屬性ng-bind我猜你想放棄angularJS應用程序,你正在使用硒網站驅動程序。

角度問題是,頁面加載後由javascript呈現。所以你想要廢棄的開始元素可能不在那裏。解決方案可能需要等待一會兒,直到角色完成渲染,然後嘗試查找元素。

driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS); 
WebElement pubCount = driver.findElement(By.xpath("//strong[@ng-bind='summary.PublishedIssuedRecent']")).getText(); 
+0

是的,xpath是正確的,在渲染時發出問題,感謝您的評論 – Prabu

1

XPath表達式本身是正確的,它會按預期選擇節點。與xmllint一個簡單的測試,得出:

> xmllint --xpath "//strong[@ng-bind='summary.PublishedIssuedRecent']" ~/test.html 
<strong class="text-xl ng-binding" ng-bind="summary.PublishedIssuedRecent">5</strong> 

因此錯誤是你的XPath表達式之外。

順便說一句,如果你只需要節點的文字,你可以使用

> xmllint --xpath "//strong[@ng-bind='summary.PublishedIssuedRecent']/text()" ~/test.html 
5