Selenium WebDriver WebElement Commands
WebElement Commands :
Given are some of the most used webelement commands.
This command is used to click on any webElement.
CODE SNIPPET
WebElement element = driver.findElement(By.id("SaveButton"));
element.click();
//Or can be written as
driver.findElement(By.id("SaveButton")).click();
This command is used to pass the text in input field. It accepts the string parameter.
Command
element.sendKeys("text");
CODE SNIPPET
WebElement element = driver.findElement(By.id("SearchBox"));
element.sendKeys("BookOfNetwork");
//Or can be written as
driver.findElement(By.id("SearchBox")).sendKeys("BookOfNetwork");
This command is used to clear the text in input field.
CODE SNIPPET
WebElement element = driver.findElement(By.id("Password"));
element.clear();
//Or can be written as
driver.findElement(By.id("Password")).clear();
This is quite similat to click command. The submit command is applicable only for (form) and makes handling of form easier. It can be used with any element inside a form.
CODE SNIPPET
WebElement element = driver.findElement(By.id("SubmitButton"));
element.submit();
//Or can be written as
driver.findElement(By.id("SubmitButton")).submit();
This command is used to get the visible text of that webElement. It returns String value.
Command
element.getText();
CODE SNIPPET
WebElement element = driver.findElement(By.id("signupModalButton"));
String text = element.getText();
This command is used to get the attribute value. It accepts String as parameter and returns String value.
Command
element.getAttribute("attributeName");
CODE SNIPPET
WebElement element = driver.findElement(By.id("Password"));
String attValue = element.getAttribute("id"); //This will return "Password"
This is used to get the position of that webelement. It returns point.
Command
element.getLocation();
CODE SNIPPET
WebElement element = driver.findElement(By.id("SubmitButton"));
Point point = element.getLocation();
System.out.println("X cordinate : " + point.x + "Y cordinate: " + point.y);
This command is used to get the tagname. It returns String value.
Command
element.getTagName();
CODE SNIPPET
WebElement element = driver.findElement(By.id("SaveButton"));
String tagName = element.getTagName();
//Or can be written as
String tagName = driver.findElement(By.id("SaveButton")).getTagName();
This is used to get the size of that element. It returns dimensions.
Command
element.getSize();
CODE SNIPPET
WebElement element = driver.findElement(By.id("SubmitButton"));
Dimension dimensions = element.getSize();
System.out.println("Height :" + dimensions.height + "Width : "+ dimensions.width);
This command is mostly used for radiobutton and checkbox, to check it is selected or not. It accepts nothing as parameter and returns a boolean.
Command
element.isSelected();
CODE SNIPPET
WebElement element = driver.findElement(By.id("Sex-Female"));
boolean status = element.isSelected();
//Or can be written as
boolean staus = driver.findElement(By.id("Sex-Female")).isSelected();
This command is used to check that element is active or not. It accepts nothing as parameter and returns a boolean.
Command
element.isEnabled();
CODE SNIPPET
WebElement element = driver.findElement(By.id("UserName"));
boolean status = element.isEnabled();
//Or can be written as
boolean staus = driver.findElement(By.id("UserName")).isEnabled();
This command is used to check the visibility of element. It accepts nothing as parameter and returns a boolean.
Command
element.isDisplayed();
CODE SNIPPET
WebElement element = driver.findElement(By.id("Password"));
boolean status = element.isDisplayed();
//Or can be written as
boolean staus = driver.findElement(By.id("Password")).isDisplayed();