有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将2个xpath合并为一个

Html

<li class="wdappchrome-ai" data-automation-id="searchInputAutoCompleteResult" tabindex="-2" aria-posinset="1" aria-setsize="3">
<span data-automation-id="searchInputAutoCompleteResultFullText">
    <span style="font-weight:500">
        <span data-automation-id="searchInputAutoCompleteResultToken" style="font-weight:400">mike</span>
        <span data-automation-id="searchInputAutoCompleteResultToken" style="font-weight:400">diana</span>
    </span>
</span>
</li>

我将用户全名分为spans,因此我想构建xpath,它将从单个list返回两个/多个span的组合文本

目前,我正在使用2个或更多的xpath,基于用户名称划分为span

我使用://li[.//span[text()='mike']]//li[.//span[text()='diana']]

然后我使用java语法对上述xpath和merge进行gettext()。因此,我正在寻找一种解决方案,它可以为我提供上面特定li标记中所有span的全文


共 (2) 个答案

  1. # 1 楼答案

    尝试以下XPath以其中一个跨度匹配元素:

    //li[.//span='diana' or .//span='mike']
    

    或通过两个跨度:

    //li[.//span='diana' and .//span='mike']
    
  2. # 2 楼答案

    要在单行中获取特定li标记中所有跨度的全文,可以使用以下代码块:

    ArrayList<String> nameList = new ArrayList<String>();
    List<WebElement> firstName_lastname = driver.findElements(By.xpath("//li[@class='wdappchrome-ai']//span[@data-automation-id='searchInputAutoCompleteResultToken']"));
    for (WebElement fN_lN:firstName_lastname)
        nameList.add(fN_lN.getAttribute("innerHTML"));
    System.out.println(nameList);