TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Selenium WebDriver - WebElement Commands

Selenium WebDriver - WebElement Commands with Introduction, features, selenium basic terminology, what is selenium, selenium limitations, selenium vs qtp, tool suite, selenium ide, ide-installation, ide-features, ide-first test case, ide-commands, ide-creating test cases manually, ide-login test etc.

<< Back to SELENIUM

Selenium WebDriver - WebElement Commands

Before proceeding with this section, first we should know the basic terminology related to web elements in WebDriver.

What is Web Element?

The term web element refers to a HTML element. The HTML documents are composed of HTML elements. It consists a start tag, an end tag and the content in between. For instance, a HTML element is written as: "<tagname> content </tagname>"

In WebDriver, we have several commonly used web element commands and actions. The following screenshot displays the eclipse web element command panel.

Selenium WebDriver - WebElement Commands

Note: To get the web element object, we have to write the statement as:

WebElement element = driver.findElement(By.id("UserName"));

Here, the UserName is the value of the id attribute, used as a unique identification for the desired web element.

Given are some of the most commonly used WebElement commands for Selenium WebDriver.

1. Clear Command

Method:

clear() : void

Command:

element.clear();

Code snippet:

WebElement element = driver.findElement(By.id("UserName"));
element.clear();
             
//Or can be written as
             
driver.findElement(By.id("UserName")).clear();

2. Sendkeys Command

Method:

sendKeys(CharSequence? KeysToSend) : void

Command:

element.sendKeys("text");

Code snippet:

WebElement element = driver.findElement(By.id("UserName"));
element.sendKeys("JavaTpoint");
             
//Or can be written as
             
driver.findElement(By.id("UserName")).sendKeys("JavaTpoint");

3. Click Command

Method:

click() : void

Command:

element.click();

Code snippet:

WebElement element = driver.findElement(By.linkText("javaTpoint"));
element.click();
             
//Or can be written as
             
driver.findElement(By.linkText("javaTpoint")).click();

4. IsDisplayed Command

Method:

isDisplayed() : boolean

Command:

element.isDisplayed();

Code snippet:

WebElement element = driver.findElement(By.id("UserName"));
boolean status = element.isDisplayed();
             
//Or can be written as
             
boolean staus = driver.findElement(By.id("UserName")).isDisplayed();

5. IsEnabled Command

Method:

isEnabled() : 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();
             
//Or can be used as
WebElement element = driver.findElement(By.id("userName"));
boolean status = element.isEnabled();
// Check that if the Text field is enabled, if yes enter value
if(status){
element.sendKeys("javaTpoint");
}

6. IsSelected Command

Method:

isSelected() : boolean

Command:

element.isSelected();

Code snippet:

WebElement element = driver.findElement(By.id("Sex-Male"));
boolean status = element.isSelected();
             
//Or can be written as
             
boolean staus = driver.findElement(By.id("Sex-Male")).isSelected();

7. Submit Command

Method:

submit() : void

Command:

element.submit();

Code snippet:

WebElement element = driver.findElement(By.id("SubmitButton"));
element.submit();
             
//Or can be written as
             
driver.findElement(By.id("SubmitButton")).submit();

8. GetText Command

Method:

getText() : String

Command:

element.getText();

Code snippet:

WebElement element = driver.findElement(By.xpath("anyLink"));
String linkText = element.getText();

9. GetTagName Command

Method:

getTagName() : String

Command:

element.getTagName();

Code snippet:

WebElement element = driver.findElement(By.id("SubmitButton"));
String tagName = element.getTagName();
             
 //Or can be written as
             
 String tagName = driver.findElement(By.id("SubmitButton")).getTagName();

10. getCssValue Command

Method:

getCssvalue() : String

Command:

element.getCssValue();

11. getAttribute Command

Method:

getAttribute(String Name) : String

Command:

element.getAttribute();

Code snippet:

WebElement element = driver.findElement(By.id("SubmitButton"));
  String attValue = element.getAttribute("id"); //This will return "SubmitButton"

12. getSize Command

Method:

getSize() : Dimension

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);

13. getLocation Command

Method:

getLocation() : 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);




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf