WebElements in Selenium


WebElements in Selenium


Selenium provides IWebElement interface through which user controls the elements in the page.

The IWebElement type exposes the following members:

  • Displayed - Gets a value whether or not this element is displayed.
  • EnabledGets a value indicating whether or not this element is enabled.
  • Location - Gets a point object containing the coordinates of the upper-left corner of this element relative to the upper-left corner of the page.     
  • Selected - Gets a value indicating whether or not this element is selected.
  • Size - Gets a Size object containing the height and width of this element.
  • TagName - Gets the tag name of this element. 
  • Text - Gets the inner Text of this element, without any leading or trailing whitespace, and with other white space collapsed.

The Methods available are as follows

  1.  Clear- Clears the content of this element.
  2.  Click- Clicks this element.       
  3.  FindElement- Finds the first IWebElement using the given method.
  4.  FindElements- Finds all the  IWebElement within the current context using the given                                         mechanism
  5.  GetAttribute- Gets the value of the specified attribute for this element.           
  6.  GetCssValue- Gets the value of a CSS property of this element.
  7.  SendKeys -  Simulates typing text into the element.      
  8.  Submit  - Submits this element to the web server.            

findElement method of WebDriver returns WebElement.
 The findElement() method accepts something as a Parameter/Argument and which is By Object. By is the mechanism used to locate elements within a document with the help of locator value.


Locating Element using By Strategy
Locating elements in WebDriver is done by using the findElement(By.locator()) method.
 The findElement methods take a locator or query object called ‘By’.
"BY" strategies in c# are listed below:

  1. By ID
    • id(String id) : By – This is the most efficient and preferred way to locate an element, as most of the times IDs are unique.
    •  It takes a parameter of String which is a Value of ID attribute and it returns a BY object to findElement() method.
    • With this strategy, If no element has a matching id attribute, a NoSuchElementException will be raised.
    • Command – driver.findElement(By.id(“Element ID”));
    • Example: WebElement element = driver.findElement(By.id("submit"));
  2. By Name
    • name(String name) : By – This is also an efficient way to locate an element but again the problem is same as with ID that UI developer make it having non-unique names on a page or auto-generating the names. 
    • It takes a parameter of String which is a Value of NAME attribute and it returns a BY object to findElement() method.
    • With this strategy, the first element with the name attribute value matching the location will be returned. If no element has a matching name attribute, a NoSuchElementException will be raised.
    • Command – driver.findElement(By.name(“Element NAME”));
    • Example:WebElement element = driver.findElement(By.name("firstname"));
  3. By ClassName
    • className(String className) : By – This finds elements based on the value of the CLASS attribute. It takes a parameter of String which is a Value of CLASS attribute and it returns a BY object to findElement() method.
    • If an element has many classes then this will match against each of them.
    • Example: WebElement parentElement = driver.findElement(By.className("button"))
  4. By TagName
    • tagName(String name) : By – With this you can find elements by their TAGNAMES. It takes a parameter of String which is a Value of TAG attribute and it returns a BY object to findElement() method.
    • Command – driver.findElement(By.tagName(“Element TAGNAME”));
    • Example: WebElement element = driver.findElement(By.tagName("button"));
  5.  By LinkText & PartialLinkText
    • linkText(String linkText) : By – With this you can find elements of “a” tags(Link) with the link names. Use this when you know link text used within an anchor tag. It takes a parameter of String which is a Value of LINKTEXT attribute and it returns a BY object to findElement() method.
    • Command – driver.findElement(By.linkText(“Element LINKTEXT”));

      Command – driver.findElement(By.partialLinkText(“Element LINKTEXT”));
  6. By XPath
    • xpath(String xpathexpression) : By – It is most popular and majorly used locating element technique or the easiest way to locate element in WebDriver. It takes a parameter of String which is a XPATHEXPRESSION and it returns a BY object to findElement() method.
    • Command-driver.findElement(By.xpath(“Element XPATHEXPRESSION”));
    • Example:driver.findElement(By.xpath(“.//*[@id='Email']”));

SendKeys
  • The send Keys method is used to send keystrokes to web elements such as textbox,password box etc.
  • Syntax: element.SendKeys(String text);
  • Example:element.SendKeys("abc");



Program: Login to Facebook account with email as"abc@mail.com" and password as "junk"

using OpenQA.Selenium; 
using OpenQA.Selenium.Chrome; 
using OpenQA.Selenium.Support.UI;
 using System;
 namespace hello_world
 {
    class Program 
     { 
         static void Main(string[] args)
           { //open facebook page
              IWebDriver driver = new ChromeDriver();  
              driver.Navigate().GoToUrl("https://www.facebook.com/"); 
              
              //Find email field and send abc@mail.com      
              IWebElement email = driver.FindElement(By.Id("email"));
              email.SendKeys("abc@mail.com");                                                            

              //Find password field and send junk
              IWebElement pass = driver.FindElement(By.Id("pass"));
              pass.SendKeys("junk"); 

              //Click on Login button
              IWebElement submit_btn = driver.FindElement(By.Id("u_0_l"));
              submit_btn.Click(); 
           }
     }
 }

In our next tutorial let us learn about Implicit and Explicit wait methods in selenium!

No comments:

Post a Comment