Skip to main content

You are here

Check if a page element is enabled or not

The following code checks if an item is enabled. If an element is not enabled then certain operations like Click() cannot be performed on it. The following code

1. Calls the browser
2. Load craigslist website
3. Create a web element variable to reference the page item we want to check. "elementToCheck"
4. creates a boolean (True/false) variable (boo) that is used to check if the located element is enabled. (Note the use of the method "isEnabled")
5. System now prints the results of this test (it will be True in this case because the element is enabled)
6. Click the element if its enabled
7. Quit

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class IsElementEnabledOrNot {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://london.craigslist.co.uk/");
WebElement elementToCheck = driver.findElement(By.xpath("//*[@id='hhh']/h4/a"));
boolean boo = elementToCheck.isEnabled(); // is it enabled?
System.out.println(boo); // tell us true or false depending on if it enabled
if(boo) elementToCheck.click();  // click it if its enabled
//driver.quit();  // uncomment if you wnat to close the browser but you might miss the whole click() show.
}

}

Category: 

Language: 

0
No votes yet

Add new comment

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.

Disclaimer

Note that the codes and ideas expressed on this site are the authors personal views. The content of this website are the personal notes based on the learning experience of the author. The author will not be responsible for any error or damages resulting from you using the content of this website.

Premium Drupal Themes by Adaptivethemes