2017-09-15 79 views
-1

我陷入了使用Selenium TestNG自動執行當前項目中的其中一項功能。 有一種情況,我想要獲取文本(您輸入的答案不正確。)從源代碼中給出的div。我使用element.getText()方法來獲取內容,但我無法獲得它攜帶的文本。任何幫助,將不勝感激。感謝先進。如何從包含其他WebElement和實際文本的div中獲取文本?

<div class="error"> 
<h2 class="visually-hidden">Error message</h2> 
<div class="icon"> 
<svg version="1.1" height="24" viewBox="0 0 64 64"> 
</div> 
The answer you entered was not correct. 
</div> 
+0

你可以分享你的XPath或定位器使用? – Murthi

+0

使用 '$( '錯誤')。文本()' 可能是它幫助.. –

+0

@Murthi感謝您的快速反應,我使用CSS選擇器(.error) –

回答

-1

您應該罰款:

// Assuming driver is instance of your WebDriver object 
driver.findElement(By.cssSelector("div.error")).getText(); 
0

您可以使用此得到測試...

String text= driver.findElement(By.xpath(".//*[@class='error']/h2/div")).getText(); 
//print text 
相關問題