How to work with a web element in Selenium WebDriver

In this article we will see how can we work with all the property of an web element found by the driver. This is necessary when we need to check the values of the properties of a web element to test.(i.e. If we want to check data, font, style,arrangement, indexing etc of a combo box selector) and to perform any action on the element. I am using c#(VS2010).

In the previous post, we see an element(IWebElement type) can be found using driver
So, the declaration code will be 
private IWebDriver driver = new FirefoxDriver();
private IWebElement element;
//In the method we will use like following
element = driver.FindElement(By.XPath("String"));// we can use any of eight way to find element.
So, we have an element, if we compare it's different properties with known values, we can prove that the element is tested.

- To Clear the content of the element
element.Clear();
 - To Click the element.
element.Click();
 -To Submit the element to the web server.(i.e- log In button) 
element.Submit();

-To write text into the element(i.e- text box)
element.SendKeys("String");

-To Get a value(true/false) indicating whether or not the element is displayed.
element.Displayed;

-To Get a value(true/false) indicating whether or not the element is enabled.
element.Enabled;
-To Get a value(true/false) indicating whether or not the element is selected
element.Selected;

-To Get the value(as string) of the specified attribute for this element.
element.GetAttribute("attribute Name as string");
-To Get the value(as string) of a CSS property of this element
element.GetCssValue("CSS Name as string");

-To Get a value(true/false) indicating whether the element is empty(use this to find an element's existence) 
element.Location.IsEmpty;
 -To Get the x-coordinate of Selected element(in Integer)
element.Location.X;
 -To Get the y-coordinate of Selected element(in Integer)
element.Location.Y;

- To Get integer(in pixel) value of the Vertical length of the element
element.Size.Height;
- To Get integer(in pixel) value of the Horizontal length of the element
element.Size.Width;
-To Get a value(true/false) indicating whether the element has width and height as Zero(0)
element.Size.IsEmpty;

-To Get the tag name of the element.
element.TagName;

-To Get the Inner Text of the element(No beginning or ending whitespace, collapsed other whitespaces)
element.Text;

Note : The basic difference to use "element" and "selectElement" is, element can be found from a page and work directly with a web component, where as selectElement ca work with page's web components as well as with there's inner items.

..Thanks... :)

No comments:

Post a Comment