LoveLovelyJava one
LoveLovelyJava one

Reputation: 353

clicking on the bottom of the list gets the element from top of the list

I am using Java and Selenium to write a test. Somewhere in the targeted application, i have a list, so I need to click on an element at the bottom of a list. I use the code below:

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='4']"))).click();

the DOM for that list looks like:

<div class="verde-items services-items">
    <div class="verde-item service" data-service="271">
        <div role="button" data-selector-btn="">
            <span class="verde-item-name service-name">1</span>
        </div>
    </div>
    <div class="verde-item service" data-service="329">
        <div role="button" data-selector-btn="">
            <span class="verde-item-name service-name">2</span>
        </div>
    </div>
    <div class="verde-item service" data-service="269">
        <div role="button" data-selector-btn="">
            <span class="verde-item-name service-name">3</span>
        </div>
    </div>
    <div class="verde-item service" data-service="270">
        <div role="button" data-selector-btn="">
            <span class="verde-item-name service-name">4</span>
        </div>
    </div>  
</div>

the problem is that when I use chrome it works but when I use firefox it scrolls down to the bottom of the list where the element is BUT it tries to click on top of the list so I get the error:

Element is not clickable at point (781, 9). Other element would receive the click:

Upvotes: 1

Views: 161

Answers (1)

Saurabh Gaur
Saurabh Gaur

Reputation: 23825

As you're saying this element is at bottom which to be visible after scrolling.

Selenium tries to scroll to the element and then click on it. This is because Selenium will not interact with an element unless it thinks that it is visible.

Scrolling to the element happens implicitly so you just need to find the item and then work with it as below :-

WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='4']")));

((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);

wait.until(ExpectedConditions.elementToBeClickable(element)).click();

Upvotes: 1

Related Questions