Solved ElementClickInterceptedException
Table of content
ElementClickInterceptedException: Element not clickable
ElementClickInterceptedException might have occurred when you are trying to click an element, the element could be a button, radio button or checkbox, or just a click operation.
ElementClickInterceptedException occurs when the target element that you want to click is overlaid by some other element in the web page.
Exception in thread "main" org.openqa.selenium.ElementClickInterceptedException:
element click intercepted:
Element <input value="Google Search" aria-label="Google Search" name="btnK" type="submit">
is not clickable at point (596, 368).
Other element would receive the click: <span>...</span>
For example, when you have an element below the dropdown/ Submenu; then you might have faced this kind of error. Sometimes some model pop-ups also can cause this kind of overlay on the element, so when you try to click such kind of element then you will receive an element not clickable exception.
In the below example, we are going to click on the Google search button after entering the term Facebook on the search bar. In the below image you can see, the auto suggestions are overlaid on the Google search button.Reasons for Element Click Intercepted Exception problem
- Selenium/Automation tools work much faster than the response of the application
- Sometimes, the speed of the internet and system hardware makes the application to run slower
- The developer might have added a parent element for the target element
- Poor design of the website
Ways that do not solve the problem:
- Clicking the element again
- Waiting using sleep
- Waiting till element becomes clickable
Solution:
We have to trigger the click operation using the JavaScript executor.
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "D:\\PATH\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://google.com");
driver.findElement(By.name("q")).sendKeys("facebook");
WebElement searchButton = driver.findElement(By.xpath("(//input[@value='Google Search'])[2]"));
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click()", searchButton);
}
JQuery to handle ElementClickInterceptedException
Instead of using the selenium find methods, you can also use JQuery methods to find the element and then click it.
$x means find element using xpath
driver.get("https://google.com");
driver.findElement(By.name("q")).sendKeys("facebook");
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("$x(\"(//input[@value='Google Search'])[2]\").click()");
Pop Ups in Selenium
Pop-Ups in Selenium
In HTML pages, we have different kinds of pop-ups; we will discuss them in this tutorial. Different popups will have different properties.
Types of Pop-Ups :
- Alert Pop Up
- Confirmation Pop Up
- Authentication Pop Up
- Hidden-Division Pop Up
- Calendar Pop Up
- Download Pop UP
- Upload Pop Up
We have a different section for Alert and Confirmation Pop-Ups, so in this page, we are going to discuss how to handle remaining Pop-Ups in selenium
Authentication Pop Up handling in selenium
When we open password-protected pages, we tend to get Authentication pop up. Authentication pop up will have username and password fields, the UI look of the pop up may vary browser to browser
Visit URL selenium webdriver Auth : https://chercher.tech/auth, the site expects you to provide credentials.
Credentials :
username - selenium
password - webdriver
Authentication Popup :
Properties of the Authentication Pop up :
- Pop up displayed on Page load
- We can move the pop up (except in chrome)
- We cannot inspect the pop up with browser inspection tools like (TryXpath or chrome dev tools)
- The look varies from browser to browser
The Solution to Authentication Pop Up : We have to pass the user name and password along with the URL to handle the authentication pop in webdriver. Please find the syntax to pass the username and password
driver.get(protocol://Usename:Password@URL Address);
Protocols: Http, Https, Ftp.
To access the https://chercher.tech/auth page, you need to pass username and password like below.
driver.get(https://selenium:webdriver@chercher.tech/auth);
Complete program to handle Authentication popup
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class AuthenticationPopUp {
public static void main(String[] args) throws Exception {
// set the geckodriver.exe property
System.setProperty("webdriver.gecko.driver", "C:/~/geckodriver.exe");
// open firefox
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
// open webpage
driver.get("https://selenium:webdriver@chercher.tech/auth");
// verify the title
if(driver.getTitle().equals("Authentication Successful")){
System.out.println("Test Passed");
}else{
System.out.println("Test failed");
}
}
}
If login is successful, you will see the below page on the browser.
Limitations:
- Does not work for the https protocol
Hidden division Pop Up with Selenium
Hidden division pop is nothing but HTML code which is hidden initially, hidden division pop up also known as dialog or overlay.
The overlay is triggered when an application user performs specific tasks like clicking a button, submitting a form, or on page load...
Examples :
- Calender Popups
- Contact forms
- Error and success Messages
Properties of Hidden division Pop up :
- Cannot be moved here and there
- We can inspect the overlay
- This is not javaScript popup
- We can resize and customize the content of the pop-up
- If the content is more then the pop-up size, pop shows scroll bar
- When hidden division pop is opened, pop takes the focus from the application.
- When pop up is closed, focus automatically goes to the application
- Hidden division popup could be nested, i.e. a hidden division pop can have another hidden division pop up
- Hidden division pop can hold other pop-ups/ alerts on it.
Xpath in Selenium
Handle Hidden division Pop Up :
1. Navigate to : https://chercher.tech/practice/hidden-division-popup
2. Click on View Pop-Up button
3. Application opens a Model
4. Write xpath for the Name text bar : //input[@type='text']
5. Send text for the Name, using sendKeys in selenium.
6. No Special Operation required to handle hidden division popup.
Complete program for handling hidden division pop up in selenium
public class HiddenDivisionPopUp {
public static void main(String[] args) throws Exception {
// set the geckodriver.exe property
System.setProperty("webdriver.gecko.driver", "C:/~/geckodriver.exe");
// open firefox
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
// open webpage
driver.get("https://chercher.tech/practice/hidden-division-popup");
driver.findElement(By.className("cd-popup-trigger")).click()
// send text to Name field on overlay
driver.findElement(By.xpath("//input[@type='text']")).sendKeys("Hidden Division Text");
}
}
Even though pop up content is present but pop is not present on UI until we click a button, for this reason, it is called as hidden division popup.
Limitations:
- We cannot perform an operation on the webpage until we close the pop up if we try to access selenium throws ElementNotClickable exception
Alerts in Selenium
Alerts in Selenium
I. Alert introduction
Alert takes the focus away from the current window and forces the browser to read the message. Alert prevents the user from accessing other parts of the web page until the box is closed.
Alert Types :
- Alert
- Confirmation dialog
- Prompt
- We cannot identify alerts using inspect tools
- We cannot write xpaths for alerts
- It is not a Window
II. Common methods with alert
1. Switch to alert popup
Alert ale = driver.switchTo().alert();
2. Accept the popUp by clicking the OK button
ale.accept();
3. Dismiss the popUp by clicking the 'X' icon.
ale.dismiss();
4. Get popUp text by getText method
ale.getText();
5. Send a text to popUp by sendKeys (Applicable for Prompt only)
ale.sendKeys("test Text");
II. Alert
The alert()
method displays an alert box with a message and an OK button; an alert box is often used to inform a user about a particular operation like details saved in Database, right-click disabled, Loaded successfully such kind of messages.
Alert is formed using alert("message")
in javascript, alert considers the same irrespective of user operation whether he clicks OK or 'X' icon.
Alert's sole purpose is to inform the user, nothing more.
Alert on Different browsers
Chrome:
Firefox
- Open Url : https://chercher.tech/practice/practice-pop-ups-selenium-webdriver
- Click on the Alert button; the application throws an Alert box
- Switch to the alert using
driver.switchTo().alert()
, we save this object in Alert type variable 'ale' - We can accept the alert by using accept() non-static method from the alert API, and this closes the popup.
driver.get("https://chercher.tech/practice/practice-pop-ups-selenium-webdriver");
driver.findElement(By.name("alert")).click();
Alert ale = driver.switchTo().alert();
// clicks 'OK' button
ale.accept();
- We can dismiss the popup using
dismiss()
method from alert API, this click 'X' icon on the popup. (if you have performed the above step then you cannot perform this step as pop up is closed in step 4 itself)
Alert ale = driver.switchTo().alert();
// clicks 'x' icon
ale.dismiss();
- We can get the text from the pop-up using getText(), if you have performed steps 4 or 5, this step will not work.
Alert ale = driver.switchTo().alert();
// clicks 'x' icon
ale.getText();
III. Confirmation Box
A Confirmation box is the second kind of alert; it displays a dialog with the OK and Cancel button. The confirmation box informs the developer about user choice whether the user pressed OK or Cancel. The confirm()
method returns true if the user clicked "OK", and false otherwise('X' icon and 'Cancel') to the developer.
Confirmation box
We can handle the Confirmation box in selenium like an alert box; there is no coding difference.
IV. Prompt
Prompt is used to get value from the user in text format. Prompt provides a text bar for user input; Prompt is rarely used alert type.
Prompt also follows the same coding as alert and prompt except the sendkeys method, and we can send a text to prompt text bar using the sendkeys() method in alerts API.
driver.get("https://chercher.tech/practice/practice-pop-ups-selenium-webdriver");
driver.findElement(By.name("prompt")).click();
Alert ale = driver.switchTo().alert();
ale.sendKeys("chercher.tech");
V. How to check whether the alert is present or not
We can use Explicit wait / WebdriverWait to check whether the alert is there or not, alertIsPresent()
method waits for the alert to be present till the given timeout, once it reaches a timeout, and if the alert is not present, then it throws TimeOutException if the alert is present before the timeout it proceeds with remaining code.
I have used only 2 seconds to check for the alert.
public static boolean isAlertPresent(){
boolean presenceOfAlert = false;
WebDriverWait wait = new WebDriverWait(driver, 2 /*timeout in seconds*/);
try {
wait.until(ExpectedConditions.alertIsPresent());
presenceOfAlert = true;
} catch (TimeoutException e) {
presenceOfAlert = false;
}
return foundAlert;
}