当我搜索某个词时,有时会立即显示搜索结果。我需要等待 10 秒才能单击该元素,即使在它之前找到了结果。
如何在搜索结果显示后立即点击该元素?
最佳答案
你可以使用这个:
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.elementToBeClickable(By.id<locator>));
示例可能是这样的:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.id("elementId")));
此代码将等待元素可点击至少 10 秒。您可以在文档 here 中获取更多信息.
根据你的解释,你可以这样做:
List<WebElement> listSearchResults = new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfAllElementsLocatedBy((By.xpath(""))));
if(listSearchResults.size() > 1){
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(listSearchResults.get(listSearchResults.size()-1))).click();
}
关于java - 如果在selenium java中等待时间到期之前找到一个元素,如何单击该元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51306103/