Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium clicks an element 'successfully', yet, it is not actually clicked

I have a method that clicks on a button, however, when it is run, selenium returns the outcome as clicked successfully, when, in reality, the button is not actually clicked. If I run the test several times, occasionally, it'll be clicked as expected. I have my test framework set as an implicit wait for about 15 seconds, I have set an explicit wait for this element, and still see the same issue. When I do <element>.isDisplayed(), the element is always found. I placed the .click in a while loop to click it a few times which works most of the time, however, still sometimes the test fails. Is it possible to have an if statement to check if an element is actually displayed before clicking the button?

I've tried:

if(!element.isDisplayed){
    element.click
}

Here is the button I am having issues with:

<button class="notkoButton listNew">
<div class="l6e iconAdd">New List</div>
</button>

Here is my method:

public marketing_lists_page navigateToNewListPage() throws Throwable {
    try {
        int x = 0;
        while(x < 5) {
            newListBtn.click();
            x++;
        }
       //newListPageHeader.isDisplayed();
    } catch (NoSuchElementException e){
        logs.errorDetails("Could not navigate to New List Page");
        Assert.fail();
    }
    return this;
}
like image 687
Saleh Qadan Avatar asked Nov 10 '15 20:11

Saleh Qadan


People also ask

How do you check whether the element is clicked or not in Selenium?

We can check if the element is clickable or not in Selenium webdriver using synchronization. In synchronization, there is an explicit wait where the driver waits till an expected condition for an element is met. To verify, if the element can be clicked, we shall use the elementToBeClickable condition.

Why click is not working in Selenium?

We can list the most common reasons for click problems as being one of the following: Wrong web element locations. The existence of a web element that obscures the web element that we want to click. The Selenium WebDriver works much faster than the response of the application.

Why are elements not clickable?

The exception “Element is not clickable at point” might be thrown when the element is not under focus or the action is being performed on the incorrect WebElement. In such cases, you have to switch to the actual element and perform the click action.

What is alternative for click in Selenium?

Alternative of click() in Selenium We can use the JavaScript Executor to perform a click action. Selenium can execute JavaScript commands with the help of the executeScript method.


2 Answers

Try scrolling to the element before clicking on it. This mostly happens when you test on chrome. You can use JavaScriptExecutor to scroll.

Something like this:

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollTo(0," + element.getLocation().Y + ")"); 
like image 81
Prat0567 Avatar answered Oct 30 '22 04:10

Prat0567


Try this, Click using javascript and feel free to change the locate for the element according to your convenience:-

WebElement element= driver.findElement(By.xpath("YOUR XPATH"));

JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);

Hope it will help you :)

like image 27
Shubham Jain Avatar answered Oct 30 '22 02:10

Shubham Jain