元素在点处不可点击 - 其他元素将接收点击

时间:2018-05-16 07:20:34

标签: java selenium webdriver

我的网站上有元素,如下所示

        <div id="wrapperCategory" class="categoryItemWrapper">
        <div class="panel panel-default panel-categoryItem text-center categoryItem disabledItem" ng-class="category.CategoryStyleClass" ng-click="setselectedProduct(productIndex,product,category); setselectedProductAmount(null);" title="Category">
            <div class="panel-heading">
                Category
            </div>
            <div class="panel-body">
                Price
            </div>
            <div class="panel-footer availability" ng-class="category.AvailabilityStyleClass">
                <span ng-bind-html="category.AvailabilityText">Avail</span>
            </div>
        </div>
    </div>

我需要点击它才能前进。如果我手动点击这些div中的每一个或者在span网站上继续前进,但SeleniumWebdriver无法点击其中任何一个。 每次出现错误时,我都尝试点击span和on div并使用ng-click事件buch:

Exception in thread "main" org.openqa.selenium.WebDriverException: Element <div class="panel panel-default panel-categoryItem text-center categoryItem" ng-class="category.CategoryStyleClass" ng-click="setselectedProduct(productIndex,product,category); setselectedProductAmount(null);" title="Category"></div> is not clickable at point (619.2666625976562, 474.23333740234375). Other element would receive the click: <div style="top: 0;left: 0;bottom: 0;right: 0;position: fixed;pointer-events: auto !important;z-index: 10000;" id="cover-1526454140024"></div>

我不明白。 我可以以某种方式检查哪个元素是可点击的,哪个元素与我要点击的元素重叠(我不会在网站代码中看到id =“cover-1526454140024”的任何div?)

更新: 不幸的是,在您的解决方案之后仍然无效。 尝试点击时会出现同样的异常。

//    try {
//      Thread.sleep(1000);
//    } catch (InterruptedException e) {
//      e.printStackTrace();
//    }
    JavascriptExecutor js = (JavascriptExecutor) Mundial.driver;
    js.executeScript("arguments[0].scrollIntoView();", categoryItem);

    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(categoryItem));
    categoryItem.click();
    List<WebElement> selects = driver.findElements(By.tagName("select"));
    Select ticketsToSelect = new Select(selects.get(3));
    ticketsToSelect.selectByValue("number:2");

它只适用于我放入睡眠并手动向下滚动的情况。我不明白。

5 个答案:

答案 0 :(得分:2)

根据您的回复:

您必须向下滚动才能让网页元素可用于您的脚本。为此,您可以使用此代码:

public static void scrollDown(WebDriver driver, String YoffSet){
            JavascriptExecutor jse = (JavascriptExecutor)driver;
            jse.executeScript(YoffSet);
    }

在这里,您可以从代码中调用此方法。

然后您可以使用此代码与Web元素进行交互:

WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("static ID")));  
element.click();

答案 1 :(得分:0)

我经历了很多这样的问题,正如Ankur所说,在点击之前使用等待

WebElement seems_unclickable = wait.until(ExpectedConditions.elementToBeClickable(By.id(...

答案 2 :(得分:0)

其中一种方法是ExpectedConditions命令

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("Yourid")));

更多示例可在http://toolsqa.com/selenium-webdriver/wait-commands/

找到

答案 3 :(得分:0)

尝试“ scrollIntoView”。

public void ScrollElementIntoView(IWebElement element)
    {
        var js = driver as IJavaScriptExecutor;
        js.ExecuteScript("arguments[0].scrollIntoView()", element);
    }

答案 4 :(得分:0)

我遇到了同样的问题,这是由于对页面正文应用了非100%缩放,例如:

body {
  zoom: 90%;
}

移除css缩放可解决此问题。