1.

How to create Web elements in selenium?

Answer»

WebElement is HTML DOM(document object model) element. When a browser loads HTML document, WebElement CONVERTS into the document object. WebElement is a class in selenium WebDriver and converts an object into the document object. Text Box, text, button, link table, radio button, checkbox, ETC, present on the webpage is webelement. Selenium needs to identify these elements uniquely before performing any action on webelement. WebElement commands will apply to almost all DOM elements on the page and used to interact with visible as well as invisible elements present on-page.

Below are some important WebElement Methods -

1. Clear(): Clear() command will clear the value in the text box if the element is text entry element. No parameters required for this method.

//Create WebElement WebElement clearElement = driver.findElement(By.id("id value")); // Clear text clearElement.clear(); // Or driver.findElement(By.id("id value")).clear()

2. SendKeys: This mimics typing and sets the value into an element. Character sequence is the parameter required for this method and returns nothing. This method used for text entry elements like INPUT and Text Area elements.

//Create WebElement WebElement sendkeyElement = driver.findElement(By.id("id value")); // Send text sendkeyElement.sendKeys("Sample Text"); //OR driver.findElement(By.id("id value").sendKeys("Sample Text"));

3. Click():  To click any element on a web page, Click() command is used and is the most COMMON way of interacting with web elements like link, radio buttons, checkboxes, etc. No parameters required for this method.

//Create WebElement WebElement clickElement = driver.findElement(By.id("id value")); //Click on an element clickElement.click(); //OR driver.findElement(By.id("id value")).click();

4. isDisplayed(): To check if an element is currently displayed or not? isDisplayed() command is used. No parameter is required with this command and returns a boolean value. If an element is present on the page, it will return true else throws 'NoSuchElementFound exception' exception.

//Create WebElement WebElement  displayElement = driver.findElement(Bt.id("id value")); //perform operation displayElement.isDisplayed(); //Or driver.findElement(By.id("id value")).isDisplayed();

5. isEnabled():  To check if an element is currently enabled or not? isEnabled() command is used. No parameter is required with this command and returns a boolean value. If an element is enabled on the page, it will return true.

//Create WebElement WebElement enabledElement = driver.findElement(By.id("id value")); //perform operation e.g. on radio button or checkbox enabledElement.isEnabled(); //Or driver.findElement(By.id("id value")).isEnabled();

6. isSelected(): To determine whether the element is selected or not, isSelected() method is used. No parameter is required with this command and returns a boolean value. If an element is currently selected or checked, it will return true. This method applies to input elements like Checkboxes, radio buttons, etc.

//Create WebElement WebElement selectedElement = driver.findElement(By.id("id value")); selectedElement.isSelected(); //Or driver.findElement(By.id("id value")).isSelected();

7. Submit(): If an element is a form or an element within a form, Submit() method works well. No parameter is required with this command and returns nothing. As an impact of Submit(), current web page changes, Submit() method will wait till new page loads.

//Create WebElement WebElement submitElement = driver.findElement(By.id("id value")); //Perform submit submitElement.Submit(); //Or driver.findElement(By.id("id text").Submit();

8. getText(): getText() method fetch visible inner text of the element. No parameter is required to pass but returns string value.

//Create WebElement WebElement gettextElement = driver.findElement(By.id("id text")); //perform getText operation gettextElement.getText(); //Or driver.findElement(By.id("id value")).getText();

9. getTagName(): This method returns the tag name of the web element. No parameter is required to pass.

//Create WebElement WebElement tagnameElement = driver.findElement(By.id("id value")); //perform operation tagnameElement.getTagName(); //Or driver.findElement(By.id("id value")).getTagName();

10. getAttribute(): This method returns the value of the given attribute of the element. A string is accepted as a parameter and returns String value.

//Create WebElement WebElement attributeElement = driver.findElement(By.id("id value")); //perform operation attributeElement.getAttribute("Attribute"); //Or driver.findElement(By.id("id value")).getAttribute("Attribute");

11. getCssValue(): This method returns CSS property value of the given element. No parameter is required to pass and return a string value.

//create webelement WebElement cssElement = driver.findElement(By.cssSelector("css value")); cssElement.getCssValue();

12. getSize(): This method returns the width and HEIGHT of the element. No parameter is required to pass and return the size of the element on the page.

//Create WebElement WebElement sizeElement = driver.findElement(By.id("id value")); Dimension dimensions = element.getSize(); System.out.println("Height :" + dimensions.height + "Width : "+ dimensions.width);

13. getLocation(): To locate location of the element getLocation() method is used. No parameter is required to pass but returns the Point object, from which we get X and Y coordinates.

//create WebElement WebElement LocationElement = driver.findElement(By.id("Btn")); Point pnt = element.getLocation(); System.out.println("X coordinate : " + point.x + "Y coordinate: " + point.y);

Let us see, the EXAMPLE of creating WebElement on facebook.com -

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_Create { public static void main(String[] args) throws InterruptedException { WebDriver driver = new FirefoxDriver(); // Wait to load page   driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); // Set driver path System.setProperty("webdriver.gecko.driver", "C:\\geckodriver.exe"); // Redirect to url driver.get("https://facebook.com"); //============ clear() ==================== // Create WebElement WebElement clElement = driver.findElement(By.id("email")); // Perform clear operation clElement.clear(); // ========== sendKeys() ================= // Create WebElement WebElement keyElement = driver.findElement(By.id("email")); // Perform sendKeys operation keyElement.sendKeys("Anderson@gmail.com"); //============ click()===================== // Create WebElement WebElement clickElement = driver.findElement(By.id("u_0_2")); // Perform click operation clickElement.click(); //============ isDisplayed() ============= // Create WebElement WebElement displayElement = driver.findElement(By.id("u_0_2")); // Perform isDisplayed operation String disp = displayElement.isDisplayed(); System.out.println(disp); //========== isEnabled() =============== // Create WebElement WebElement enabledElement = driver.findElement(By.id("u_0_2")); // Perform isEnabled operation String ena = enabledElement.isEnabled();  System.out.println(ena); //=========== isSelected() =========== // Create WebElement WebElement selectedElement = driver.findElement(By.id("u_0_2")); // Perform eleSelected operation String tSelect = eleSelected.isSelected(); System.out.println(tSelect ); // ============ submit() ========== // Create WebElement WebElement submitElement = driver.findElement(By.id("u_0_15")); // Perform submit operation submitElement .submit(); // =========== getText() =============== // Create WebElement WebElement textElement = driver.findElement(By.id("u_0_2")); // Perform getText operation textElement.getText(); //=========== getTagName() ============== // Create WebElement WebElement tagnameElement = driver.findElement(By.id("u_0_2")); // Perform getTagName operation String tName = tagnameElement.getTagName(); System.out.println(tName); //========== getAttribute() =============== // Create WebElement WebElement attributeElement = driver.findElement(By.id("u_0_2")); // Perform getgetAttribute operation String attri = attributeElement.getAttribute("Attribute"); System.out.println(attri); // ============ cssSelector() ================= // Locating textBox element using CSS Selector WebElement signButton = driver.findElement(By.cssSelector("#u_0_15 ")); System.out.println(signButton); // =============== getSize() =================== WebElement dimensionsElement = driver.findElement(By.id("u_0_15")); Dimension dimensions = dimensionsElement.getSize(); System.out.println("Height :" + dimensions.height + "Width : " + dimensions.width); // ======= getLocation() ======================== WebElement locationElement = driver.findElement(By.id("u_0_15")); Point point = locationElement.getLocation(); System.out.println("X coordinate : " + point.x + "Y coordinate: " + point.y);         } }


Discussion

No Comment Found