1

我有一个搜索框,当我搜索一个值时,比如银行名称,它会显示与搜索框中给出的文本匹配的结果。但这里的问题是它可能不是一个确切的结果,而且通过在搜索结果中智能刷新,加载完整列表需要几秒钟。因此,即使我等待加载某些结果,它也会立即获取当时出现的值,但实际上它会在几秒钟后刷新列表。那么我怎样才能等待完成加载然后取出结果。请对此有任何帮助。

在此处输入图像描述

代码片段

<ul class="DropDown__options DropDown__options--small--focused DropDown__options--focused" id="searchTopBank-listbox" role="listbox" aria-activedescendant="searchTopBank-options-4">
    <li aria-selected="true" class="DropDown__option TypeAhead__option DropDown__option--focused DropDown__option--small" id="searchTopBank-options-0" role="option">
        <img alt="" class="dropdown-logo" src="data">
        <span>
            <span class="DropDown__matched-option-chars">Bank of America</span>
        </span>
    </li>
    <li aria-selected="false" class="DropDown__option TypeAhead__option DropDown__option--focused DropDown__option--small" id="searchTopBank-options-1" role="option">
        <img alt="" class="dropdown-logo" src="data">
        <span>First National 
            <span class="DropDown__matched-option-chars">Bank of America</span>
        </span>
    </li>
    <li aria-selected="false" class="DropDown__option TypeAhead__option DropDown__option--focused DropDown__option--small" id="searchTopBank-options-2" role="option">
        <img alt="" class="dropdown-logo" src="data:image/png;base64, null">
        <span>Second National Farmer's 
            <span class="DropDown__matched-option-chars">Bank of America</span>
        </span>
    </li>
    <li aria-selected="false" class="DropDown__option TypeAhead__option DropDown__option--focused DropDown__option--small" id="searchTopBank-options-3" role="option">
        <img alt="" class="dropdown-logo" src="data:image/png;base64, null">
        <span>
            <span class="DropDown__matched-option-chars">Altabank</span>
        </span>
    </li>
    <li aria-selected="false" class="DropDown__option TypeAhead__option DropDown__option--focused DropDown__option--hover DropDown__option--small" id="searchTopBank-options-4" role="option">
        <img alt="" class="dropdown-logo" src="data:image/png;base64, null">
        <span>Freedom 
            <span class="DropDown__matched-option-chars">Bank of America</span>
        </span>
    </li>
</ul>
4

2 回答 2

1

我建议做这样的事情:

WebDriverWait wait = new WebDriverWait(driver, 20);

public void wait(int delay){
    try {
        Thread.sleep(delay);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

String searchResult = "//li[contains(@id,'searchTopBank-options')]";
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(searchResult)));
int initialAmount = driver.findElements(By.xpath(searchResult)).size();
while(true){
    wait(200);
    int newAmount = driver.findElements(By.xpath(searchResult)).size();
    if(newAmount > initialAmount){
        initialAmount = newAmount;
    }else{
        break;
    }
}

等待量取决于带来新搜索结果的最慢实际速度。

于 2021-06-24T14:44:34.643 回答
0

为什么不混合最差ExplicitWait-(Thread.sleep(5000))并将其与可靠的混合ExplicitWait -visibilityOfAllElements并使用 - 给出列表driver.findElements

我相信这个css代表了所有元素:

span.DropDown__matched-option-chars

代码 :

Thread.sleep(5000);
new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfAllElements(driver.findElements(By.cssSelector("span.DropDown__matched-option-chars"))));
于 2021-06-24T14:35:05.180 回答