Beginning PowerShell for SharePoint Follow Us! Latest Books. Articulate Storyline Essentials 18 June Beginning SharePoint Development 18 June Beginning SharePoint 18 June Popular Categories. The class attribute is provided to apply CSS to an element. In this example, the login form elements use the class attribute instead of the id attribute:.
We can use the class attribute to locate elements in the following way:. Selenium WebDriver API provides the findElement method to locate the elements that are required in a test from the page under test. When locating an element matching specified criteria, it looks through the DOM Document Object Model for matching elements and returns the first matching element to the test.
The WebElement class also supports find methods that find child elements. For example, imagine that there are some duplicate elements on a page. The findElement and FindElements methods throw up the NoSuchElementFoundException exception when they fail to find the desired element using the specified locator strategy.
Selenium WebDriver provides the findElements method, which enables the acquisition of a list of elements matching the specified search criteria. This method is useful when we want to work with a group of similar elements. For example, we can get all the links displayed on a page or get rows from a table, and so on. In this recipe, we will get all the links and print their targets by using the findElements method. Let's create a test which will get all the links from a page and verify the count of links and print target for each link as follows:.
The findElements method returns all the elements matching with the locator specified as a list of WebElements. In Java, we can use the List class to create an instance of list of WebElements. The size method of the List class will tell us how many elements are there in the list. We can iterate using this list in the following way, getting a link and printing its target value:.
The Locating an element using the findElement method recipe. Selenium WebDriver provides multiple ways to locate links. You can locate a link either by its text or by partial text. Locating links with partial text comes in handy when links have dynamic text. In this recipe, we will see how to use these methods to locate the links on page. Let's create a sample test to see how locating links work in Selenium WebDriver with the following options. Selenium WebDriver's By class provides the linkText method to locate links using the text displayed for the link.
In the following example, we will locate the GMail link:. Selenium WebDriver's By class also provides a method to locate links using partial text. This method is useful where developers create links with dynamic text.
In this example, a link is provided to open inbox. This link also displays the number of new e-mails which may change dynamically. Here we can use the partialLinkText method to locate the link using a fixed or known portion of the link text, in this case it would be inbox. The linkText and partialLinkText locator methods query the driver for all the links that meet the specified text and returns the matching link s.
You can also locate links using id , name , or class attributes if developers have provided these attributes. Locating elements based on text can cause issues while testing applications in multiple locales. Using parameterized text locator value could work in such applications. This comes in handy when you want to locate elements using their tag name. In this recipe, we will briefly see how to use the tagName locator method. Let's assume you have a single button element on a page. You can locate this button by using its tag in the following way:.
We can do this in the following way:. The tagName locator method queries the DOM and returns a list of matching elements for the specified tag name. This method may not be reliable while locating individual elements and the page might have multiple instances of these elements.
CSS was introduced to keep the presentation information separate from the markup or content. These patterns, called selectors, may range from simple element names to rich contextual patterns. If all conditions in the pattern are true for a certain element, the selector matches the element and the browser applies the defined style in CSS syntax.
This is a much faster and more reliable way to locate the elements when compared with XPaths. CSS absolute paths refer to the very specific location of the element considering its complete hierarchy in the DOM. Here is an example where the Username Input field is located using the absolute path. While providing absolute path, a space is given between the elements.
However, this strategy has limitations as it depends on the structure or hierarchy of the elements on a page. If this changes, the locator will fail to find the element. With relative path we can locate an element directly, irrespective of its location in the DOM. This is same as the className and id locator methods. However, there is another strategy where we can use any other attribute of the element that is not covered in the By class. While finding elements using the CSS selector, we can use the Class attribute to locate an element.
This can be done by specifying the type of HTML tag, then adding a dot followed by the value of the class attribute in the following way:. There is also a shortcut where you can put a. However, this will return all the elements with class as login and the test may not return the correct element.
We can locate the element using the IDs assigned to elements. This can be done by specifying the type of HTML tag, then entering a hash followed by the value of the Class attribute, as shown:. There is also a shortcut where you can enter and a class attribute value and ignore the HTML tag. However, this will return all the elements with the id set as username and the test may not return the correct element. This has to be used very carefully. Apart from the class and id attributes, CSS selectors also enable the location of elements using other attributes of the element.
Using the name attribute to locate an element is similar to the name locator method of the By class. Let's use some other attribute to locate an element. You might come across situations where one attribute may not be sufficient to locate an element and you need to combine additional attributes for a precise match. This strategy is a bit different from the earlier strategy where we want to locate elements based on only the specific attribute defined for them but not attribute values.
A Boolean not pseudo-class can also be used to locate elements not matching the specified criteria. CSS selector provides a way to locate elements matching partial attribute values. This is very useful for testing applications where attribute values are dynamically assigned and change every time a page is requested. For example, ASP. NET applications exhibit this kind of behavior, where IDs are generated dynamically. The following table explains the use of CSS partial match syntax:.
The majority of browsers support CSS parsing for applying styles to these elements. CSS selectors provide various methods, rules, and patterns to locate the element from a page. This is also a more reliable and fast method when compared with XPath locators. Using CSS selector, the test can locate elements in multiple ways using Class, ID, attribute values, and text contents as described in this recipe.
The XPath language is based on a tree representation of the XML document and provides the ability to navigate around the tree, selecting nodes using a variety of criteria. Locating elements with XPath works very well with a lot of flexibility. However, this is the least preferable locator strategy due its slow performance. This means that with XPath we can locate a parent element using a child element. In this recipe, we will explore some basic XPath queries to locate elements and then examine some advanced XPath queries.
Selenium WebDriver provides the xpath method for locating elements using XPaths. Here is an example where Username Input field is located using the absolute path.
While providing absolute path a space is given between the elements. If this changes, the locator will fail to get the element. With relative path, we can locate an element directly irrespective of its location in the DOM.
There could be multiple elements matching the specified XPath query. If the element is not the first element, we can also locate the element by using its index in DOM. Finding elements using attributes values with XPath. In the following example, the Username field is located using the ID attribute:. Here is another example where the image is located using the alt attribute:.
You might come across situations where one attribute may not be sufficient to locate an element and you need combined additional attributes for a precise match.
In the following example, either of the attributes is used to locate the elements using XPath or operator:. This strategy is a bit different from the earlier strategy where we want to locate elements based only on the specific attribute defined for them but not attribute values. This is very useful for testing applications where attributes values are dynamically assigned and change every time a page is requested.
NET applications exhibit this kind of behavior where IDs are generated dynamically. The following table explains the use of these XPath functions:. XPath matches the attribute for all the elements for a specified value and returns the element.
For example, in the following XPath query, 'userName' is specified. XPath will check all the elements and their attributes to see if they have this value and return the matching element. XPath axes help to locate elements based on the element's relationship with other elements in a document.
This can be applied to any other element structure from your application. Selects all descendants children, grandchildren, and so on of the current node. Selects everything in the document after the closing tag of the current node. This will get the second column from the second row immediately after the column that has Product 1 as the text value.
Checking an element's CSS values How to do it See also Performing double-click on an element How to do it See also Performing drag-and-drop operations How to do it See also Executing JavaScript code How to do it Capturing screenshots with Selenium WebDriver How to do it Maximizing the browser window Getting ready How to do it Automating dropdowns and lists How to do it See also Checking options in dropdowns and lists Getting ready How to do it See also Checking selected options in dropdowns and lists Getting ready How to do it See also Automating radio buttons and radio groups How to do it Automating checkboxes How to do it Controlling Windows processes How to do it See also 3.
See also Synchronizing a test with an explicit wait How to do it See also Synchronizing a test with custom-expected conditions How to do it Waiting for element's attribute value update Waiting for an element's visibility Waiting for DOM events Checking an element's presence How to do it See also Checking an element's status How to do it Identifying and handling a pop-up window by its name How to do it There is more See also Identifying and handling a pop-up window by its content How to do it See also Handling a simple JavaScript alert How to do it See also Handling a prompt box alert How to do it See also Identifying and handling frames How to do it See also Identifying and handling frames by their content How to do it See also 4.
Creating a data-driven test in Python How to do it Using the LoadableComponent class Getting ready How to do it Implementing nested Page Object instances Getting ready How to do it
0コメント