我无法从搜索结果列表中检索第二个搜索结果的标题属性文本



我正在尝试从下面的代码集中获取标题文本"Interactions/Rant & Rave Survey Scores/CSS Call Volumes":

<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);
}
}

我试图检索的是标题文本"Interactions/Rant & Rave Survey Scores/CSS Call Volumes",这是第二个搜索结果。但我从第一个结果中获得了标题。

我在想,因为我正在使用搜索结果来查找下一个元素,它应该一次保存一个搜索项目,但这里的情况并非如此。

请指教。

试试这个

WebElement tooltip =searchResult.findElement(By.xpath("/div[@class='treeNode']/span/span"));

由于您正在寻找直接的儿童div/div/span/span

另外,您是否需要遍历外部元素"div"的所有元素/子元素? 如果没有,那么您可以将 hirarchy 与所有元素一起使用,直到跨度(如果将来没有更改此 hirarchy 的风险(

/

在Java中有一个特殊的引用。可能是它导致了问题。

我还没有尝试过,但请尝试以下代码,它可能会有所帮助:

String test[];
List<WebElement> searchResults = driver.findElements(By.xpath("//div[@id='$searchResults_children']/div"));
for (WebElement searchResult : searchResults)
{
String item = searchResult.getText();
WebElement tooltip =searchResult.findElement(By.xpath("//div[@class='treeNode']/span/span"));
test = tooltip.getText().split("/");
for (String searchResult1 : test) 
{
System.out.println(searchResult);
}
String tooltipName = tooltip.getAttribute("title");
System.out.println("Mertic: "+item+", Tooltip: "+tooltipName);
}

您希望获取此元素的title属性

<span id="$InteractionsAHT Call FactsTransfer Attempts_details" class="treeNodeDetails" title="Interactions/Inbound Call Facts/AHT Call Facts">

它有一个 ID,因此我们可以使用它来定位它,然后拉取title属性。

driver.findElement(By.cssSelector("span[id$='Attempts_details']")).getAttribute("title");

此定位器查找 ID 以 ($( 结尾的Attempts_detailsSPAN。我们可以只指定整个 ID,但它很长,而且这个子字符串是唯一的,至少在提供的 HTML 中是这样。


我假设你得到了错误的元素,因此是一个不完整的标题。我猜它与将其写入控制台有关?它应该有效,因为正斜杠没有特殊意义。您是否尝试过将其写入文本文件或在该行上放置断点并查看变量包含的内容?

正如您在评论中提到的,the id $searchResults_children is static for any search result以下代码块将从所需节点检索文本交互/入站呼叫事实/AHT 呼叫事实

String my_string = driver.findElement(By.xpath("//div[@id='$searchResults_children']/div[@class='treeNode'][contains(@id, 'FactsTransfer Attempts')]/span[@class='masterTreeLine treeLine']//span[@class='treeNodeDetails']")).getAttribute("title");
System.out.println(my_string);

相关内容

  • 没有找到相关文章

最新更新