[英]how to get Text inside the Div and button tag
<div class="content-wrapper" style="min-height: 611px;">
<div class="alert alert-success alert-dismissable">
<button class="close" type="button" data-dismiss="alert" aria-hidden="true">×</button>
The new customer role has been added successfully.
</div>
如何在 Button 標簽后獲取此文本。 我嘗試了很多方法,但我無法得到它。
//div[@class='alert alert-success alert-dismissable']
您可以使用//div[@class='alert alert-success alert-dismissable']/text()
xpath 定位文本The new customer role has been added successfully.
但是 selenium 不支持 xpath 中的text()
方法來定位文本節點。
嘗試下面的代碼從
<button>
標簽中獲取文本。
String button = driver.findElement(By.xpath("//button[@class='close'][@type='button']")).getText();
System.out.println(button);
嘗試以下代碼以獲取文本
The new customer role has been added successfully.
JavascriptExecutor js = (JavascriptExecutor)driver;
Object str = js.executeScript("var value = document.evaluate(\"//button[@class='close']/following::node()[contains(., 'The new customer role has been added successfully.')]\",document, null, XPathResult.STRING_TYPE, null); return value.stringValue;");
System.out.println(str.toString());
您可以從這里詳細探索
請參閱下圖以供參考。
我不確定你的 HTML,但你可以試試這個 XPath
//*[@class='alert alert-success alert-dismissable']
使用 java getText()
將從 html 中獲取您的文本
driver.findElement(By.xpath(" //*[@class='alert alert-success alert-dismissable']")).getText();
讓我知道 XPath 是否有效
我想你已經嘗試過下面的 xpath
//*[@class='alert alert-success alert-dismissable']
您能否檢查一下警告框(我假設它是給定的 html 中的警告框)是否作為新窗口彈出。 在這種情況下,您需要切換到新窗口。
還要檢查警報彈出窗口是否有覆蓋屬性
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.