Home > Software engineering >  Selenium : Alert handling
Selenium : Alert handling

Time:01-02

I am trying to click on a button which will prompt a modern alert. But, I am getting "element is not clickable at a point " error.

    driver.get("https://letcode.in/alert");
    //send keys
    driver.findElement(By.id("prompt")).click();
    alert.sendKeys("Praveen");
    alert.accept();
    System.out.println(driver.findElement(By.id("myName")).getText());
    
    //modern
    driver.findElement(By.id("modern")).click();
    Thread.sleep(1000);
    driver.findElement(By.xpath("//button[@aria-label='close']")).click();

If I do not perform the send keys prompt. The //modern alert works perfectly. I am facing this error after the execution of //send keys alert.

Please help

CodePudding user response:

The error "element is not clickable at a point" usually indicates that the element you are trying to click on is not visible or is covered by another element on the page. This can happen if the page is still loading or if the element you are trying to click on is inside an overlay or modal that is not yet visible.

To fix this error, you can try using one of the following approaches:

Wait for the page to fully load before interacting with the elements on it. You can use the WebDriverWait class to wait for a specific condition to be true before proceeding with the script.

Use the Actions class to move the mouse to the element you want to click on before clicking it. This can sometimes help if the element is being covered by another element on the page.

Copy code Actions actions = new Actions(driver); actions.moveToElement(driver.findElement(By.id("modern"))).click().perform(); Use the javascriptExecutor to click on the element.

 WebElement element = driver.findElement(By.id("modern"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);

I hope these suggestions help! If you are still having trouble, it might be helpful to see the full code that you are using, as well as any error messages or stack traces that you are seeing.

CodePudding user response:

A good practice is to wait for element to be clickable before trying to actually click. Try adding a wait before clicking:

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

This will wait for 10 seconds for the element to be clickable before clicking.

You will need to import WebDriverWait and ExpectedConditions from org.openqa.selenium.support.ui.

  • Related