Lal Ansari
Lal Ansari

Reputation: 139

The IE element is not getting clicked in the first time using Selenium WebDriver

I am trying to click on a link in IE 11, and using the below code:

driver.findElement(By.xpath("//a[text()='En savoir plus']")).click();

I am not getting any exception but the page is not getting navigated anywhere, it also freezes the whole page and I am not able to continue.

I encountered the same issue few years back and the solution I can recall was to use the same command twice:

driver.findElement(By.xpath("//a[text()='En savoir plus']")).click();
driver.findElement(By.xpath("//a[text()='En savoir plus']")).click();

This would click on the link succesfully without freezing the page.

Is there any solution for this problem?

Upvotes: 2

Views: 163

Answers (2)

Jainish Kapadia
Jainish Kapadia

Reputation: 2611

Try this below code, using javascript executor method.

Note:- Before going to click on this button, provide few seconds of wait so your driver may able to find the webelement.

For wait I am using Explicit Wait method.

new WebDriverWait(driver, 60).until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//a[text()='En savoir plus']"))));                 //wait for 60 seconds.
WebElement button = driver.findElement(By.xpath("//a[text()='En savoir plus']"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", button);

Upvotes: 2

Reez0
Reez0

Reputation: 2687

Maybe this will help?

         try {
    WebElement yourElement = driver.findElement(By.xpath("//a[text()='En savoir plus']"));
                    if (yourElement.isEnabled() && yourElement.isDisplayed()) {
                        System.out.println("Clicking on element with using javascript click");

     ((JavascriptExecutor) driver).executeScript("arguments[0].click();", yourElement);

} else {
                System.out.println("Unable to click on element");
            }
        } catch (StaleElementReferenceException e) {
            System.out.println("Element is not attached to the page document "+ e.getStackTrace());
        } catch (NoSuchElementException e) {
            System.out.println("Element was not found in DOM "+ e.getStackTrace());
        } catch (Exception e) {
            System.out.println("Unable to click on element "+ e.getStackTrace());
        }
    }
}

Upvotes: 1

Related Questions