本文介紹了如何在 Selenium WebDriver 中獲取單個文本節點的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我想從標簽中獲取文本,但沒有嵌套標簽中的文本.IE.在下面的示例中,我只想從 <small>
標記中獲取字符串 183591
并排除文本 Service Request ID:
來自 <span>
標簽.這不是微不足道的,因為 <span>
標記嵌套在 <small>
標記中.這可以通過 WebDriver 和 XPath 實現嗎?
I want to get the text from a tag but without the text from nested tags. I.e. in the example below, I only want to get the string 183591
from inside the <small>
tag and exclude the text Service Request ID:
from the <span>
tag. This is not trivial because the <span>
tag is nested in the <small>
tag. Is this possible with WebDriver and XPath?
標簽中的文字每次都會改變.
The text in the tag is going to change every time.
<div id="claimInfoBox" style="background-color: transparent;">
<div class="col-md-3 rhtCol">
<div class="cib h530 cntborder">
<h4 class="no-margin-bottom">
<p>
<small style="background-color: transparent;">
<span class="text-primary" style="background-color: transparent;">Service Request ID:</span>
183591
</small>
</p>
<div class="border-bottom" style="background-color: transparent;"></div>
<div id="CIB_PersonalInfo_DisplayMode" class="cib_block">
<div id="CIB_PersonalInfo_EditMode" class="cib_block" style="display: none">
</div>
</div>
<script type="text/javascript">
</div>
</div>
推薦答案
您將不得不使用字符串操作.比如:
You are going to have to use String manipulation. Something like:
// you will need to adjust these XPaths to suit your needs
String outside = driver.findElement(By.xpath("http://small")).getText();
String inside = driver.findElement(By.xpath("http://span")).getText();
String edge = outside.replace(inside, "");
這篇關于如何在 Selenium WebDriver 中獲取單個文本節點的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!