我想從下面的一組代碼獲得標題文本「互動/誇大其詞&狂歡調查分數/ CSS呼叫量」檢索的第二個搜索結果標題屬性文字:我無法從搜索結果列表
<div id="$searchResults_children" class="treeChildContainer" style="margin-left: 0px; display: block;">
<div id="$InteractionsAHT Call FactsTransfer Attempts" class="treeNode" style="display: block;">
<span class="masterTreeLine treeLine" style="display: block;">
<img id="$InteractionsAHT Call FactsTransfer Attempts_disclosure" src="res/v-0YH2UYTNo3k/s_Alta/uicomponents/obips.Tree/disclosure_collapsed.png" alt="" class="treeNodeDisclosure" style="visibility: hidden;">
<span id="$InteractionsAHT Call FactsTransfer Attempts_details" class="treeNodeDetails" title="Interactions/Inbound Call Facts/AHT Call Facts">
<img src="res/v-0YH2UYTNo3k/s_Alta/uicomponents/obips.Tree/measure.png" alt="" class="treeNodeIcon">
<span class="treeNodeText">Transfer Attempts</span>
</span>
</span>
<div id="$InteractionsAHT Call FactsTransfer Attempts_children" class="treeChildContainer"></div>
</div>
<div id="$InteractionsCSS Call VolumesTransfer %" class="treeNode" style="display: block;">
<span class="masterTreeLine treeLine" style="display: block;">
<img id="$InteractionsCSS Call VolumesTransfer %_disclosure" src="res/v-0YH2UYTNo3k/s_Alta/uicomponents/obips.Tree/disclosure_collapsed.png" alt="" class="treeNodeDisclosure" style="visibility: hidden;">
<span id="$InteractionsCSS Call VolumesTransfer %_details" class="treeNodeDetails" title="Interactions/Rant & Rave Survey Scores/CSS Call Volumes">
<img src="res/v-0YH2UYTNo3k/s_Alta/uicomponents/obips.Tree/measure.png" alt="" class="treeNodeIcon">
<span class="treeNodeText masterTreeLineSelected treeLineSelected masterTreeLineActive treeLineActive treeNodeFocusClass">Transfer %</span>
</span>
</span>
<div id="$InteractionsCSS Call VolumesTransfer %_children" class="treeChildContainer"></div>
</div>
</div>
代碼:
List<WebElement> searchResults = driver.findElements(By.xpath("//div[@id='$searchResults_children']/div"));
for (WebElement searchResult : searchResults) {
String item = searchResult.getText();
if (item.equals(searchItemName)) {
WebElement tooltip = searchResult.findElement(By.xpath("//div[@id='$searchResults_children']/div[@class='treeNode']/span[@class='masterTreeLine treeLine']/span[@class='treeNodeDetails']"));
String tooltipName = tooltip.getAttribute("title");
System.out.println("Mertic: " + item + "\nTooltip: "+ tooltipName);
}
}
我試圖找回的是標題文本「互動/誇大其詞&狂歡調查分數/ CSS呼叫量」,這是第二個搜索結果。但是我從第一個結果中得到了標題。
我一直在想,因爲我使用搜索結果來查找下一個元素,它應該一次只能容納一個搜索項,但在這裏並不是這樣。
請建議。
右遠,你正在做的一切硒去。我在c#中測試了你的代碼,它運行良好,但你沒有使用C#。這可能是斜線行爲像分隔符或什麼的問題。 tooltipName的價值是什麼? –
IMO,大部分'id'在我看來都是動態生成的id。因此,您可能無法將使用'id'的節點與'$ searchResults_children'匹配。 – DebanjanB
@ chris-crush-code我得到的tooltipName的值是「Interactions」 – TimeMuffin