Webdriver Commands & Operations

 1. Opening a New page:

 driver.get(“http://www.google.com”);

WebDriver will wait until the page has fully loaded (that is, the “onload” event has fired) before returningcontrol to your test or script. It’s worth noting that if your page uses a lot of AJAX on load then WebDriver may not know when it has completely loaded. If you need to ensure such pages are fullyloaded then you can use “waits”. More on this later.

2. Locating UI Elements & Interactions with the page:

Imagine that you have an element displayed on a page with the following code

<input type=”text” name=”Password” id=”Password-ID”/>

You can find this element by using Any of the following commands:

Locate By ID:

This is the most efficient and preferred way to locate an element.

IWebElement element;

element=driver.findElement(By.id(“Password-ID”));

Locate By Name:

Find the input element with matching name attribute.

IWebElement element;

element=driver.findElement(By.name(“Password”));

Locate By XPath:

WebDriver uses a browser’s native XPath capabilities wherever possible. On browsers that don’t have native XPath support, webdriver provides its own implementation. This can lead to some unexpected behaviour unless you are aware of the differences in the various xpath engines.

 IWebElement element;

element=driver.findElement(By.xpath(“//input[@id=’Password-ID’]”));

 Locate By Class Name:

“Class” in this case refers to the attribute on the DOM element. Often in practical use there are many DOM elements with the same class name, so be very carefull when finding elements using this option. For example, lets say that you have the following html code in your browser:

<div class="cheese"><span>Cheddar</span></div><div class="cheese"><span>Brie
</span></div>

If you use the below code, it will only target the first element it finds, i.e Cheddar.

IWebElement element;

element=driver.findElement(By.ClassName(“cheese”));

But if you want to find multiple elements, you can use the command below:

IList<IWebElement> cheeses = driver.FindElements(By.ClassName("cheese"));

 Locate by Tag Name:

You can also locate an element by its DOM tag name.

For example, say you want to find an iframe which is displayed on the page with the following code:

<iframe src="..."></iframe>

The command you will need is this:

IWebElement frame = driver.FindElement(By.TagName(“iframe”));

 Locate By Link Text:

You can find an element by matching its visible text.

Say you have a link displayed with the following code.

<a href="http://www.thetrainline.com/Hotels">Hotels</a>>

The command you will need is this:

IWebElement cheese = driver.FindElement(By.LinkText(“Hotels”));

 Locate By Partial Link Text:

You can find an element by matching its partial visible text.

Say you have a link displayed with the following code.

<a href="http://www.thetrainline.com/BestFareFinder">Best Fare Finder</a>> 

The command you will need is this:

IWebElement cheese = driver.FindElement(By.LinkText(“Fare”));

When using all of the above commands to find elements, if no element can be found then a NoSuchElementException will be thrown.

3. What to do after finding an element:

So you have found an element, what would you like to do with it?

You can enter some text in the text field:

element.SendKeys(“some text”);

 Keep in mind that typing something into a text field will not clear it. Instead, what you type will be appended to what’s already there.  You can easily clear the contents of the text field or text area:

element.Clear();

You can click an element:

After finding the element using its link text or partial link text, you can click on it.

element.Click();

Leave a Reply

Your email address will not be published.