首页 > 解决方案 > 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);
    }

有什么可能是错的?

标签: javaseleniumselenium-chromedriver

解决方案


推荐阅读