Different Actions on WebElements

conocer gente rd Actions on WebElements:

  • The getAttribute() method
  • The sendKeys() method
  • The clear() method
  • The submit() method
  • The getCssValue() method
  • The getLocation() method
  • The getSize() method
  • The getText() method
  • The getTagName() method
  • The isDisplayed() method
  • The isEnabled() method
  • The isSelected() method

In the previous section, we have seen how to locate WebElements on a web page by using different locating mechanisms.

Here, we will see all the different user actions that can be taken on a WebElement.

Different WebElements will have different actions that can be taken on them. For example, in a textbox element, we can type in some text or clear the text that is already typed in it. Similarly for a button, we can click on it, get the dimensions of it, and so on, but we cannot type into a button, and for a link, we cannot type into it. So, though all the actions are listed in one WebElement interface.

In case we try to execute a wrong action on a WebElement, we don’t see any exception or error thrown and also we don’t see any action that really gets executed; WebDriver ignores such actions silently.

http://amazingmarbella.com/?menstryaciy=club-de-rencontre.pf&80c=de The getAttribute() method

The getAttribute action can be executed on all the WebElements.

They are generally key-value pairs appearing in the start tag of an element. For example, in the following WebElement:

< this content label name=”Username” id=”uname”>Enter Username: </label>

Here name and id are the attributes or attribute keys and Username and uname are the attribute values.

sites rencontre Syntax:

java.lang.String getAttribute(java.lang.String name)

helpful site Example:

Consider Google Search Button;

<button id=”gbqfba” class=”gbqfba” name=”btnK” aria-label=”Google Search”>

helpful hints Script:

public class GetAttributes {
public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement searchButton = driver.findElement(By.name("btnK"));

System.out.println("Name of the button is: " +searchButton.getAttribute("name"));

System.out.println("Id of the button is: " +searchButton.getAttribute("id"));

System.out.println("Class of the button is: " +searchButton.getAttribute("class"));

System.out.println("Label of the button is: "
+searchButton.getAttribute("aria- label"));

}
}

In the above code getAttribute() method is to fetch the attribute values of the attribute name, id, class, and aria-label of the important site Google Search button WebElement.

click over here OutPut:

Name of the button is: btnK
Id of the button is: gbqfba
Class of the button is: gbqfba
Label of the button is: Google Search

rencontres 15 replay The sendKeys() method

The sendKeys action is applicable for textbox or textarea HTML elements.

This is used to type text into the textbox.

visit site Syntax

void sendKeys(java.lang.CharSequence…keysToSend)

The input parameter for the preceding method is CharSequence of text that has to be entered into the element. This method doesn’t return anything.

Example

Type a search text into the Google Search box using the sendKeys() method.

public class sendKeys {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys("TestingBar");
}
}

If you want to type in some special keys, such as Backspace, Enter, Tab, Shift etc. then you need to use a special enum class of WebDriver named Keys .

Example:

public class SendKeys {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys(Keys.chord(Keys.SHIFT,"TestingBar"));
}
}

In the above code, the chord() method from the Keys enum is used to type the key while the text specified is being given as an input to be the textbox. i.e Shift Key + ”Text”

So in the above program all the text being typed in uppercase.

The clear() method

The clear action is similar to the sendKeys() method, which is applicable for textbox and textarea elements.

This is used to erase the text that is entered in a WebElement using the sendKeys() method.

Syntax:

void clear();

This method doesn’t take any input and doesn’t return any output. It is simply executed on the target text entry element.

Example:

public class Clear{
public static void main(String[] args){
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys(Keys.chord(Keys.SHIFT,"testingbar"));
searchBox.clear();
}
}

WebElement’s clear() method to clear the text after typing testingbar into the Google Search box.

*This can be achieved using the Keys.BACK_SPACE enum also.

The submit() method

The submit action can be taken on a form or on an element, which is inside a form. This is used to submit a form of a web page to the server hosting the web application.

Syntax:

void submit();

The preceding method doesn’t take any input parameter and doesn’t return anything. But a NoSuchElementException is thrown when this method is executed on a WebElement that is not present within a form.

Example:

public class Submit {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys(Keys.chord(Keys.SHIFT,"testingbar"));
searchBox.submit();
}
}

In the above code search box is not a part of any form. So you should encounter a NoSuchElementException message.

So when you use the submit() method on a WebElement, make sure it is part of the form element.

The getCssValue() method

The getCssValue action can be taken on all the WebElements.

This is used to fetch the CSS properties’ values of the given element. CSS properties can be font-family, background-color, color, and so on.

Syntax:

java.lang.String getCssValue(java.lang.String propertyName)

Example:

public class GetCSSValue {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement searchButton = driver.findElement(By.name("btnK"));
System.out.println(searchButton.getCssValue("font-family"));
}
}

The getCssValue() method to find the font-family of the text visible on the Google Search button.

Output:

MS Shell Dlg

Similarly, we can retrieve the background color of an element using this method. (Refer the below Example)

public class GetCSSValue2 {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement searchButton = driver.findElement(By.name("btnK"));
System.out.println(searchButton.getCssValue("background-color"));
}
}

Output:

Transparent

The getLocation() method

The getLocation action can be executed on all the WebElements.

This is used to get the relative position of an element where it is rendered on the web page.

This position is calculated relative to the top-left corner of the web page of which the (x, y) coordinates are assumed as (0, 0).

This method will be of use if your test script tries to validate the layout of your web page.

Syntax

Point getLocation();

Example:

public class GetLocation {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement searchButton = driver.findElement(By.name("btnK"));
System.out.println(searchButton.getLocation());
}
}

Output:

(370,350)

The getSize() method

The getSize action can also be applied on all the visible components of HTML.

It will return the width and height of the rendered WebElement.

Dimension getSize()

This method doesn’t take any input parameters, and the return type is a class instance named Dimension. This class contains the width and height of the target WebElement.

Example:

public class GetSize {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement searchButton = driver.findElement(By.name("btnK"));
System.out.println(searchButton.getSize());
}
}

Output: (width, height)

(103,35)

The getText() method

The getText action can be taken on all the WebElements.

It will give the visible text if the element contains any text on it or else will return nothing.

Syntax:

java.lang.String getText();

Example:

public class GetText {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement searchButton = driver.findElement(By.name("btnK"));
System.out.println(searchButton.getText());
}
}

Output:

Google Search

The getTagName() method

The getTagName action can be taken on all the WebElements.

This will return the tag name of the WebElement.

Syntax:

java.lang.String getTagName()

Example:

<button id=”gbqfba” class=”gbqfba” name=”btnK” aria-label=”Google Search”>

Script:

public class GetTagName {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement searchButton = driver.findElement(By.name("btnK"));
System.out.println(searchButton.getTagName());
}
}

Output:

Button

The isDisplayed() method

The isDisplayed action verifies if an element is displayed on the web page and can be executed on all the WebElements.

Syntax:

boolean isDisplayed();

Example:

public class isDisplayed {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement searchButton = driver.findElement(By.name("btnK"));
System.out.println(searchButton.isDisplayed());
}
}

The isEnabled() method

The isEnabled action verifies if an element is enabled on the web page and can be executed on all the WebElements.

Syntax:

boolean isEnabled();

Example:

public class isEnabled {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement searchButton = driver.findElement(By.name("btnK"));
System.out.println(searchButton.isEnabled());
}
}

The isSelected() method

The isSelected action verifies if an element is selected right now on the web page and can be executed only on a radio button, options in select, and checkbox WebElements.

When executed on other elements, it will return false.

Syntax:

boolean isSelected()

Example:

public class IsSelected {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement searchBox = driver.findElement(By.name("q"));
System.out.println(searchBox.isSelected());
}
}

It returns false for the Google Search box, because this is not a radio button, options in select, or a checkbox.