簡體   English   中英

如何在 Div 和按鈕標簽中獲取文本

[英]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 中的警告框)是否作為新窗口彈出。 在這種情況下,您需要切換到新窗口。

還要檢查警報彈出窗口是否有覆蓋屬性

</i>

[英]How to delete the parent div when <i> tag inside button clicked?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM