Element operations or element manipulation simply means that once you find an element then you can manipulate that element as you want.
All the operations used to interact with a web page are performed through the IWebElement
Interface.
The TagName
property returns the tag name of the element, not the value of the name attribute. For example, it will return input
for an element specified by the HTML markup .
IWebElement element = driver.FindElement(By.Id("SubmitBtn"));
String tagName = element.TagName;
You can also get the tag name like this.
String tagName = driver.FindElement(By.Id("SubmitBtn")).TagName;
The Text
property returns the innerText of this element, without any leading or trailing whitespace, and with other whitespace collapsed. It accepts nothing as a parameter but returns a String value.
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.facebook.com/");
IWebElement element = driver.FindElement(By.Xpath("//*[@id='reg_pages_msg']/a"));
string linkText = element.Text;
The Enabled
property determines whether the element currently is Enabled or not? It will generally return true for everything except explicitly disabled input elements.
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.facebook.com/");
IWebElement element = driver.FindElement(By.Id("email"));
bool status = element.Enabled;
// Check that if the Text field is enabled, if yes enter value
if(status){
element.SendKeys("email id");
}
The Selected
property determines whether the element is selected or not. This operation only applies to input elements such as Checkboxes, Select Options, and Radio Buttons. It returns True if the element is currently selected or checked, false otherwise.
IWebElement element = driver.FindElement(By.Id("rememberme"));
bool status = element.Selected;
The Location
property returns a System.Drawing.Point
object containing the coordinates of the upper-left corner of this element relative to the upper-left corner of the page. It accepts nothing as a parameter but returns the Point object.
IWebElement element = driver.FindElement(By.Id("SubmitBtn"));
Point point = element.Location;
Console.WriteLine("X cordinate : " + point.X + "Y cordinate: " + point.Y);
The Size
property returns the width and height of the rendered element. It accepts nothing as a parameter but returns the Dimension object.
IWebElement element = driver.FindElement(By.Id("SubmitBtn"));
Dimension dimensions = element.Size();
Console.WriteLine(“Height :” + dimensions.Height + ”Width : "+ dimensions.Width);
The Displayed
property determines if an element is currently being displayed or not. It avoids the problem of having to parse an element's "style" attribute to determine the visibility of an element. It accepts nothing as a parameter but returns a boolean value(true/false).
IWebElement element = driver.FindElement(By.Id("email"));
bool status = element.Displayed;
The Clear()
method clears the content of the element. If the element is a text entry element, the Clear
method will clear the value. It does not affect other elements. Text entry elements are defined as elements with input
or textarea
tags.
IWebElement element = driver.findElement(By.Id("email"));
element.Clear();
The Click()
method clicked the element. If the click causes a new page to load, the Click
method will attempt to block until the page has loaded.
Click
method, you should discard all references to this element unless you know that the element and the page will still be present.IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.facebook.com/");
IWebElement element = driver.FindElement(By.LinkText("Create a Page"));
element.Click();
The GetAttribute()
returns the value of the specified attribute for this element. The attributes are Ids, Name, Class, etc.
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.facebook.com/");
IWebElement element = driver.FindElement(By.LinkText("Create a Page"));
String attValue = element.GetAttribute("id");
The GetCssValue()
method returns the CSS property value of the given element. This accepts a string as a parameter which is the property name.
GetCssValue
method is likely to be unpredictable in a cross-browser environment.IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.facebook.com/");
IWebElement element = driver.FindElement(By.LinkText("Create a Page"));
String fontSize = element.GetCssValue("font-size");
The GetProperty()
method returns the value of a JavaScript property of the given element.
var element = driver.FindElement(By.Id("email"));
string prop = element.GetProperty("value");
The SendKeys
method simulates typing text into the element to set its value. The text to be typed may include special characters like arrow keys, backspaces, function keys, etc. This method works fine with text entry elements like input
and textarea
elements.
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.facebook.com/");
var element = driver.FindElement(By.Id("email"));
element.SendKeys("[email protected]");
The Submit()
method works well/better than the Click()
method if the current element is a form or an element within a form. If this causes the current page to change, then this method will wait until the new page is loaded.
IWebElement element = driver.FindElement(By.Id("SubmitBtn"));
element.Submit();