java - org.openqa.selenium.TimeoutException:预期条件失败:等待元素可点击
问题描述
我为单击页面上的所有菜单项和子项创建了测试。单击第二项的子项失败:org.openqa.selenium.ElementClickInterceptedException:元素单击被拦截:元素<a class="side-bar-third__link" href="/radiology/kt/golova/">...</a>
在点(456、824)不可单击。其他元素会收到点击:<div class="monitoring_link">...</div>
@Test
public void clickAllMenuItemsTest() {
System.setProperty("webdriver.chrome.driver", "libs/chromedriver/chromedriver.exe");
List<String> links = new ArrayList<>();
driver = new ChromeDriver();
driver.get("https://www.invitro.ru/radiology/");
JavascriptExecutor jsExecutor = driver;
WebDriverWait wait = new WebDriverWait(driver, 10);
final By locator = By.cssSelector("a.side-bar-second__link");
final By locatorActiveItem = By.cssSelector(".side-bar-second__items.side-bar__items--active");
final By locatorSubItems = By.cssSelector(" a.side-bar-third__link");
wait.until(ExpectedConditions.elementToBeClickable(locator));
int numberOfElementsFound = getNumberOfElementsFound(locator);
for (int pos = 0; pos < numberOfElementsFound; pos++) {
wait.until(ExpectedConditions.elementToBeClickable(locator));
final WebElement elementClickable = getElementWithIndex(locator, pos);
jsExecutor.executeScript("arguments[0].scrollIntoView(true);", elementClickable);
elementClickable.click();
wait.until(ExpectedConditions.elementToBeClickable(locatorActiveItem));
int numberOfSubElementsFound = getNumberOfElementsFound(locatorActiveItem, locatorSubItems);
for (int subPos = 0; subPos < numberOfSubElementsFound; subPos++) {
wait.until(ExpectedConditions.elementToBeClickable(locatorSubItems));
final WebElement subElementClickable = driver.findElement(locatorActiveItem).findElements(locatorSubItems).get(subPos);
jsExecutor.executeScript("arguments[0].scrollIntoView(true);", subElementClickable);
//fails here:
subElementClickable.click();
}
}
driver.quit();
}
private WebElement getElementWithIndex(By locatorActiveItem, By locatorSubItems, int index) {
return driver.findElement(locatorActiveItem).findElements(locatorSubItems).get(index);
}
private int getNumberOfElementsFound(By locatorActiveItem, By locatorSubItems) {
return driver.findElement(locatorActiveItem).findElements(locatorSubItems).size();
}
public int getNumberOfElementsFound(By by) {
return driver.findElements(by).size();
}
public WebElement getElementWithIndex(By by, int index) {
return driver.findElements(by).get(index);
}
有什么可能是错的?
解决方案
推荐阅读
- javascript - Vue 3 组合 API 去抖动功能
- algorithm - 是什么让这棵红/黑树变得沉重,它是否正确?
- java - 如何从数组中删除元素而不获取:ArrayIndexOutOfBoundsException?
- flutter - 如何定位图标而不溢出?我尝试通过添加填充,通过灵活包装小部件,但到目前为止没有运气
- android - 应用于背景按钮时,边框背景变为纯色
- java - IntelliJ 中的 Web.xml 映射 servlet 不起作用
- c# - 如何在 .Net 核心项目中使用 C# Web Api 阅读 pdf、替换其文本和发送邮件
- algorithm - 使用特征方程求解递归
- postgresql - top 语句的 PostgreSQL 子语句之间的数据一致性如何保证?
- gatling - Gatling:循环 x 次从馈线中提取值