Thursday, 26 March 2015

Selenium WebDriver WebElement Commands

Selenium WebDriver WebElement Commands

The next thing you like to do is to interact with a web page. You need to locate an element first on the web page before interacting with it and locating elements can be done on the WebDriver instance itself or on a WebElement. Webdriver gives us “Find Element” and “Find Elements” method to locate elements on the web page.

Find Element & Find Elements Method

The difference between “Find Element” and “Find Elements” method is the first returns a WebElement object otherwise it throws an exception and the latter returns a list of WebElements, it can return an empty list if no DOM elements match the query. The “Find” methods take a locator or query object called “By”. “By” strategies are listed below.

By ID

With this strategy, the first element with the id attribute value matching the location will be returned. If no element has a matching id attribute, a NoSuchElementException will be raised. This is the most efficient and preferred way to locate an element, as most of the times IDs are unique. But in some cases UI developers make it having non-unique ids on a page or auto-generating the id, in both cases it should be avoided.
Example: If an element is given like this:

By Name

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. 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.
Example: If an element is given like this:

By Class Name

With this you can find elements based on the value of the “class” attribute. If an element has many classes then this will match against each of them. A class can contain many elements.
Example: If an element is given like this:

By Tag Name

With this you can find elements by their tag names.
Example: If an element is given like this:

By Link Text

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.
Example: If an element is given like this:

By Partial Link Text

With this you can find elements of “a” tags(Link) with the partial link names. Use this when you know link text used within an anchor tag.
Example: If an element is given like this
Or

Practice Exercise 1

1) Launch new Browser
2) Open URL http://www.toolsqa.com/automation-practice-form/
3) Type Name & Last Name (Use Name locator)
4) Click on Submit button (Use ID locator)

Solution



Practice Exercise 2

1) Launch new Browser
2) Open URL http://www.toolsqa.com/automation-practice-form/
3) Click on the Link “Partial Link Test” (Use ‘patialLinkTest’ locator and search by ‘Partial’ word)
4) Identify Submit button with ‘tagName’, convert it in to string and print it on the console
5) Click on the Link “Link Test” (Use ‘linkTest’ locator)

Solution


No comments:

Post a Comment