WebElement Commands in selenium :
What is WebElement ?
- WebElement is an object / HTML element.
- HTML elements can be written with a start tag, end tag, with the content in between: <tagname> Web Page content </tagname>
- HTML heading for Web Page <h1> Heading </h1>
- The HTML element is everything from the start tag to the end tag: <p> paragraph </p>
WebElement Syntax :
1 2 3 4 5 6 7 | WebElement element = driver.findElement(By.id("TextBox")); element.sendKeys("stqatools"); Or driver.findElement(By.id("TextBox")).sendKeys("stqatools"); |
Demo HTML Code :
1 2 3 4 5 6 | <html> <body> <h1> Heading </h1> <p> paragraph </p> </body> </html> |
- WebElement commands in selenium will be applicable to almost all the DOM elements on the web page.
- Every single controls present on the page is called a WebElement in WebPage.
- Each of WebElement is represented in selenium by WebElement interface.
- Note: WebElement interface is used to interact with both visible and invisible elements present on a page.
Clear Command:
- clear( ) : If element is a text entry element, this will clear the value.
- This method accepts nothing as a parameter and returns nothing.
- This method has no effect on other elements, except text elements.
- Text entry elements and TextArea elements are INPUT.
1 2 3 4 5 6 7 8 9 10 | // Create WebElement WebElement eleClear = driver.findElement(By.id("TextBox")); // Perform clear operation eleClear.clear(); // OR // Clear particular WebElement e.g: Textbox. driver.findElement(By.id("TextBox")).clear(); |
SendKeys Command:
- sendKeys(“Text” ) : This simulate typing into an element, which may set its value. This method accepts Char Sequence as a parameter and returns nothing.
- This method works fine with text entry elements like INPUT and Text Area elements.
1 2 3 4 5 6 7 8 9 10 | // Create WebElement WebElement elesendKeys = driver.findElement(By.id("TextBox")); // Perform sendKeys operation elesendKeys.sendKeys("Sandeep"); // OR // Send value to particular WebElement e.g: Textbox. driver.findElement(By.id("TextBox")).sendKeys("Sandeep"); |
Click Command:
- click( ) : This command use to clicking of any element. Accepts nothing as a parameter and returns nothing.
- Clicking is the most common way of interacting with web elements like text elements, links, radio boxes and many more.
1 2 3 4 5 6 7 8 9 10 | // Create WebElement WebElement eleclick = driver.findElement(By.id("TextBox")); // Perform click operation eleclick.click(); // OR // Click on any WebElement e.g: Button. driver.findElement(By.id("Button_Id")).click(); |
isDisplayed Command:
- isDisplayed( ) : This commad is used to check if an element is currently being displayed or not. This accepts nothing as a parameter but returns boolean value(true/false).
- This will return true if the element is present on the page and throw a NoSuchElementFound exception if the element is not present on the page.
1 2 3 4 5 6 7 8 9 10 | // Create WebElement WebElement eleisDisplayed = driver.findElement(By.id("TextBox")); // Perform isDisplayed operation eleisDisplayed.isDisplayed(); // OR // Verify WebElement is Present or Not? driver.findElement(By.id("Text")).isDisplayed(); |
isEnabled Command:
- isEnabled( ) : This determines if the element currently is Enabled or not? This accepts nothing as a parameter but returns boolean value(true/false).
1 2 3 4 5 6 7 8 9 10 | // Create WebElement WebElement eleEnabled = driver.findElement(By.id("TextBox")); // Perform isEnabled operation eleEnabled.isEnabled(); // OR // Verify WebElement is Enabled or Not? e.g: Radio / Checkbox. driver.findElement(By.id("Text")).isEnabled(); |
isSelected Command:
- isSelected( ) : Determine whether or not this element is selected or not.
- This accepts nothing as a parameter but returns boolean value(true/false).
- This operation only applies to input elements such as Checkboxes, Select Options and Radio Buttons.
- This returns True if the element is currently selected or checked, false otherwise.
1 2 3 4 5 6 7 8 9 10 | // Create WebElement WebElement eleSelected = driver.findElement(By.id("TextBox")); // Perform eleSelected operation eleSelected.isSelected(); // OR // Verify WebElement is Selected or Not? e.g: Radio / Checkbox driver.findElement(By.id("Text")).isSelected(); |
Submit Command:
- submit( ) : This method works well / better than the click() if the current element is a form, or an element within a form.
- This accepts nothing as a parameter and returns nothing in WebElement commands in selenium.
- If this causes the current page to change, then this method will wait until the new page is loaded.
1 2 3 4 5 6 7 8 9 10 | // Create WebElement WebElement elesubmit = driver.findElement(By.id("TextBox")); // Perform submit operation elesubmit.submit(); // OR // Submit the form & perform action. driver.findElement(By.id("Submit")).submit(); |
GetText Command:
- getText( ) : This method will fetch the visible inner Text of the element.
- This accepts nothing as a parameter but returns a String value.
1 2 3 4 5 6 7 8 9 10 | // Create WebElement WebElement elegetText = driver.findElement(By.id("TextBox")); // Perform getText operation elegetText.getText(); // OR // Get text of Particular WebElement & Store into String driver.findElement(By.id("TextBox")).getText(); |
getTagName Command:
- getTagName( ) : This method gets the tag name of the Web Element. This accepts nothing as a parameter and returns a String value.
1 2 3 4 5 6 7 8 9 10 | // Create WebElement WebElement elegetTagName = driver.findElement(By.id("TextBox")); // Perform getTagName operation elegetTagName.getTagName(); // OR // Able to get TagName of Particular WebElement & Store into String driver.findElement(By.id("TextBox")).getTagName(); |
getAttribute Command:
- getAttribute(String Name) : This method gets the value of the given attribute of the element.
- This accepts the String as a parameter and returns a String value.
1 2 3 4 5 6 7 8 9 10 | // Create WebElement WebElement elegetAttribute = driver.findElement(By.id("TextBox")); // Perform getgetAttribute operation elegetAttribute.getAttribute("Attribute"); // OR // Able to get Attribute of Particular WebElement & Store into String driver.findElement(By.id("TextBox")).getAttribute("Attribute"); |
getCssValue Command:
- getCssvalue( ) : This method Fetch CSS property value of the give element.
- This accepts nothing as a parameter and returns a String value.
1 2 3 4 5 | //Locating textBox element using CSS Selector WebElement textBox = driver.findElement(By.cssSelector("div#textBox ")); //Performing sendKeys operation on the element textBox.sendKeys("stqatools"); |
getSize Command:
- getSize( ) : This method fetch the width and height of the rendered element.
- This accepts nothing as a parameter but returns the Dimension object.
- This returns the size of the element on the page.
1 2 3 4 5 | WebElement element = driver.findElement(By.id("Btn")); Dimension dimensions = element.getSize(); System.out.println("Height :" + dimensions.height + "Width : "+ dimensions.width); |
getLocation Command:
- getLocation( ) : This method locate the location of the element on the page. This accepts nothing as a parameter but returns the Point object.
- This returns the Point object, from which we can get X and Y coordinates of specific element.
1 2 3 4 5 | WebElement element = driver.findElement(By.id("Btn")); Point point = element.getLocation(); System.out.println("X cordinate : " + point.x + "Y cordinate: " + point.y); |
Example of WebElement Commands:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.Dimension; import org.openqa.selenium.Point; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; public class WebElement_Commands { public static void main(String[] args) throws InterruptedException { WebDriver driver = new FirefoxDriver(); // Wait For Page To Load. driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); // Set geckodriver path System.setProperty("webdriver.gecko.driver", "D:\\geckodriver.exe"); // Using get command redirect to url. driver.get("https://stqatools.com/"); // ======================= clear ========================== // Create WebElement WebElement eleClear = driver.findElement(By.id("TextBox")); // Perform clear operation eleClear.clear(); // OR // Clear particular WebElement e.g: Textbox. driver.findElement(By.id("TextBox")).clear(); // ======================= sendKeys ========================== // Create WebElement WebElement elesendKeys = driver.findElement(By.id("TextBox")); // Perform sendKeys operation elesendKeys.sendKeys("Sandeep"); // OR // Send value to particular WebElement e.g: Textbox. driver.findElement(By.id("TextBox")).sendKeys("Sandeep"); // ======================= click ========================== // Create WebElement WebElement eleclick = driver.findElement(By.id("TextBox")); // Perform click operation eleclick.click(); // OR // Click on any WebElement e.g: Button. driver.findElement(By.id("Button_Id")).click(); // ======================= isDisplayed ========================== // Create WebElement WebElement eleisDisplayed = driver.findElement(By.id("TextBox")); // Perform isDisplayed operation eleisDisplayed.isDisplayed(); // OR // Verify WebElement is Present or Not? driver.findElement(By.id("Text")).isDisplayed(); // ======================= isEnabled ========================== // Create WebElement WebElement eleEnabled = driver.findElement(By.id("TextBox")); // Perform isEnabled operation eleEnabled.isEnabled(); // OR // Verify WebElement is Enabled or Not? e.g: Radio / Checkbox. driver.findElement(By.id("Text")).isEnabled(); // ======================= isSelected ========================== // Create WebElement WebElement eleSelected = driver.findElement(By.id("TextBox")); // Perform eleSelected operation eleSelected.isSelected(); // OR // Verify WebElement is Selected or Not? e.g: Radio / Checkbox driver.findElement(By.id("Text")).isSelected(); // ======================= submit ========================== // Create WebElement WebElement elesubmit = driver.findElement(By.id("TextBox")); // Perform submit operation elesubmit.submit(); // OR // Submit the form & perform action. driver.findElement(By.id("Submit")).submit(); // ======================= getText ========================== // Create WebElement WebElement elegetText = driver.findElement(By.id("TextBox")); // Perform getText operation elegetText.getText(); // OR // Get text of Particular WebElement & Store into String driver.findElement(By.id("TextBox")).getText(); // ======================= getTagName ========================== // Create WebElement WebElement elegetTagName = driver.findElement(By.id("TextBox")); // Perform getTagName operation elegetTagName.getTagName(); // OR // Able to get TagName of Particular WebElement & Store into String driver.findElement(By.id("TextBox")).getTagName(); // ======================= getAttribute ========================== // Create WebElement WebElement elegetAttribute = driver.findElement(By.id("TextBox")); // Perform getgetAttribute operation elegetAttribute.getAttribute("Attribute"); // OR // Able to get Attribute of Particular WebElement & Store into String driver.findElement(By.id("TextBox")).getAttribute("Attribute"); // ======================= cssSelector ========================== // Locating textBox element using CSS Selector WebElement textBox = driver.findElement(By.cssSelector("div#textBox ")); // Performing sendKeys operation on the element textBox.sendKeys("stqatools"); // ======================= getSize ========================== WebElement eledimensions = driver.findElement(By.id("Btn")); Dimension dimensions = eledimensions.getSize(); System.out.println("Height :" + dimensions.height + "Width : " + dimensions.width); // ======================= getLocation ========================== WebElement elegetLocation = driver.findElement(By.id("Btn")); Point point = elegetLocation.getLocation(); System.out.println("X cordinate : " + point.x + "Y cordinate: " + point.y); } } |
WebElement Commands in selenium and WebElement Commands in selenium test WebElement Commands in selenium and selenium commands