Skip to main content

Browser Driver Element

Browser Driver Element

BrowserElement represents a DOM element. BrowserElements can be found by searching from the document root using a BrowserDriver instance, or by searching under another BrowserElement. BrowserElement implements Selenium WebElement so you can read more documentation about it on Selenium project documentation.

Get Active Element

It is used to track (or) find DOM element which has the focus in the current browsing context.

// Get attribute of current active element
String attr = driver.switchTo().activeElement().getAttribute("title");

Click

Clicks on this element.

driver.findElement(BrowserSearch.name("btnK")).click();

Is Element enabled

This method is used to check if the referenced Element is enabled or disabled on a webpage. Returns a boolean value, True if the referenced element is enabled in the current browsing context else returns false.

//returns true if element is enabled else returns false
boolean value = driver.findElement(BrowserSearch.name("btnK")).isEnabled();

Is Element Selected

This method determines if the referenced Element is Selected or not. This method is widely used on Check boxes, radio buttons, input elements, and option elements.

Returns a boolean value, True if referenced element is selected in the current browsing context else returns false.

//returns true if element is checked else returns false
boolean value = driver.findElement(BrowserSearch.cssSelector("input[type='checkbox']:first-of-type")).isSelected();

Get Element Tag Name

It is used to fetch the Tag Name of the referenced Element.

//returns TagName of the element
String value = driver.findElement(BrowserSearch.name("title")).getTagName();

Get Element Rect

It is used to fetch the dimensions and coordinates of the referenced element.

The fetched data contains the following details:

  • X-axis position from the top-lef corner of the element
  • y-axis position from the top-lef corner of the element
  • Height of the element
  • Width of the element


// Returns height, width, x and y coordinates referenced element
Rectangle res = driver.findElement(BrowserSearch.cssSelector("h1")).getRect();

// Rectangle class provides getX,getY, getWidth, getHeight methods
System.out.println(res.getX());

Get Element CSS Value

Retrieves the value of specified computed style property of an element in the current browsing context.

// Retrieves the computed style property 'color' of linktext
String cssValue = driver.findElement(BrowserSearch.linkText("More information...")).getCssValue("color");

Get Element Text

Retrieves the rendered text of the specified element.

// Retrieves the text of the element
String text = driver.findElement(BrowserSearch.cssSelector("h1")).getText();