Elements like Buttons, textBoxes, Images and Links etc… are clickable, and should be present on screen & enabled to click.
Selenium exposes isEnabled() for us to verify if an element is enabled or not [ isEnabled() returns Boolean value, true if the element is enabled, else returns false]
So we can verify if the element is enabled, then click else skip or throw exception to fail the step!
in some situations, application takes some time to enable / clickable, for this we can use Selenium explicit wait’s elementToBeClickable(WebElement el / By byLocator)
ExpectedConditions.elementToBeClickable(WebElement el / By byLocator)
Reference – ExpectedConditions class
elementToBeClickable() first verifies if the element is present & enabled, then click or enter text, else throw TimeOutException.
let’s see the implementation –
First consider a positive scenario –
On this qavalidation_demo page, Let’s verify if Full Name edit box is clickable or not and then enter some text.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import org.testng.annotations.Test; import java.time.Duration; public class ClickableTest { static WebDriver driver = null; @Test public static void Test_ClickableTest() throws InterruptedException { //Provide the Chrome driver path to send the selenium requests to browser System.setProperty("webdriver.chrome.driver", "/Users/MyMac/sel/chromedriver"); //Launch firefox browser driver = new ChromeDriver(); //maximize the browser driver.manage().window().maximize(); //Let's not use implicit wait with webDriverWait //Implicit wait, wait for at least some time (10 sec) to identify an element, // if can't find the element with in 10 sec, throw exception //driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //open the url or AUT driver.get("https://qavalidation.com/demo"); Thread.sleep(1000); System.out.println("WebDriverWait to verify if the Full Name is clickable"); try { WebElement el = new WebDriverWait(driver, Duration.ofSeconds(10)) .until(ExpectedConditions.elementToBeClickable(By.id("username"))); el.sendKeys("QAVBOX"); //if el is a button, then we can perform click operation } catch (Exception e) { System.out.println("Error from code - " + e.getMessage()); } Thread.sleep(1000); driver.quit(); } }
In the above code, as the Full Name is present and enabled, after you run above code, the test will enter “QAVBOX” into Full Name text box.
Consider an element not clickable
on the same qavalidation_demo page, let’s verify if the Fax No. field is clickable to enter text.
try { WebElement el = new WebDriverWait(driver, Duration.ofSeconds(10)) .until(ExpectedConditions.elementToBeClickable(By.id("fax"))); el.sendKeys("1234567890"); //if el is a button, then we can perform click operation } catch (Exception e) { System.out.println("Error from code - " + e.getMessage()); }
Try block will throw TimeOutException, catch block will catch the exception, and will not enter value to fax field.
If we don’t use the ExpectedConditions.elementToBeClickable(), then the script will pass even without clicking the element, so always good practice to verify if the element is clickable before performing action.
You can create a utility method [include the try / catch block], one for click and another to enter text which will accept the element as parameter.
from the other class or test, call the utility method and pass the required element to perform click or enter text.
Shortlink – bit.ly/qav-selisenabled
in C#.Net
public static Boolean IsClickable(IWebElement el, IWebDriver driver)
{
try
{
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementToBeClickable(el));
return true;
}
catch (Exception ex)
{
return false;
}
}
CALLING:
if (IsClickable(btnContinue[0], driverENAMCC))
{
executorENAMCC.ExecuteScript(“arguments[0].click();”, btnContinue[0]);
}