Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Selenium2.0 part 1: positioning elements

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Selenium WebDriver provides advanced technology to locate elements on a web page. Selenium-rich API provides a variety of strategies for locating elements, such as name, XPath ID, CSS selector, etc., as well as custom positioning methods to locate elements.

First, locate the element through the fidElement method

WebDriver and WebElement class. Provide findElement () and findElements () methods to locate elements

The findElement () method returns information about the web element through a special index, or throws an exception (no matching element is found)

The findElements () method returns a list of web elements or an empty list (to match to elements)

(1) find elements through the ID attribute

On the page, the method of locating elements through the ID attribute is the most practical. The W3C recommends that each element on the page be provided with a unique ID. This unique ID attribute can locate the element clearly and reliably.

When dealing with DOM, browsers use id as the preferred method and identification element, which provides the fastest positioning strategy.

Eg: now let's see how to use the id attribute to locate the login form for an element.

To locate the area of the user name and password, we can use the following ways of id attribute:

WebElement username = driver.findElement (By.id ("username"))

WebElement password = driver.findElement (By.id ("password"))

(2) find elements through name attribute

Locating elements through the ID attribute is the preferred strategy, but you may find that you cannot use ID to locate elements, as follows:

1) not all elements on the page have their own unique ID attributes

2) the ID attribute of the key elements on the page is not unique

3) automatic generation of ID attributes of elements

Eg: the login form replaces the ID attribute with the name attribute

You can locate an element with the name attribute in the following ways:

WebElement username = driver.findElement (By.name ("username"))

WevElement password = driver.findElement (By.name ("password"))

Unlike ID, the name attribute may not be unique on a page, and you may find different similar name attributes, in which case the first element on the page with this special value will be selected. If this element is not the one you want, it will cause an error

(3) search through the class attribute of the element

In addition to using the ID and name attributes to find elements, you can also use class attributes to locate elements. This class attribute is provided by the CSS applied on the element

Eg: the login form replaces the ID attribute with a class attribute

You can use class attributes to locate elements in the following way

WebElement username = driver.findElement (By.className ("username"))

WebElement password = driver.findElement (By.className ("password"))

The findElement () method provided by Selenium WebDriver API requires that under the test of the test page, when an element is located by a special standard, the system traverses the DOM (document object Model) to match the element and returns the first element that matches successfully.

2. Supplement

The WebElement class also provides a way to locate child elements. For example, imagine that there are a lot of repetitive elements on the page, but they are different elements. We can first locate the elements in the parent, and then locate the child elements in the context.

WebElement div = driver.findElement (By.id ("div1"))

WebElement topLink = driver.findElement (By.linkText ("top"))

NoSuchElementFoundException exception: an error is reported when the element cannot be found

Using fidElements method to locate elements

Selenium WebDriver provides the findElements () method, which can list mobile phone elements and match the search criteria. This method is very useful. If you want to locate a large number of similar elements, for example, we can get all the links displayed on the page, or get rows from a table, etc., we can use the findElements () method to get all the links and print them.

Eg: create a test to get all the link on the page, verify the number of link, and print the target for each link

The findElements () method returns the match of all elements as a WebElement list, and in JAVA you can use the List class to create an instance of WebElement

List links = driver.findElements (By.tagName ("a"))

Size () in the List class tells us how many elements there are in the list.

AssertEquals (4, links.size ()); determine whether the element in links is 4

Also iteratively use this list to get the link and print the target as follows

For (WebElement link: links)

System.out.println (link.getAttribute ("href"))

Locate an element using the findElements () method

2. Link positioning

Selenium WebDriver provides a variety of ways to locate links. We can locate links not only through the text of links, but also through part of the text. When links is in dynamic text, it is more convenient to locate links through part of the text. In this section, we will know how to use these methods to locate links on the page.

Let's give an example to verify how to locate links in selenium webdriver using the following method

(1) find links through his text

Selenium webdriver's By class provides the linksTest () method to locate links that is displayed as a link in text. In the following example, we will locate the link to Google Mailbox.

WebElement gmailLink = driver.findElement (By.linkTest ("GMail"))

AssertEquals ("http://mail.google.com/",gmailLink.getAttribute("href"));"

(2) find links through part of the text

Selenium webdriver's By class also provides a way to locate links through partial text, which is useful if developers create this link with dynamic text. In the following example, a link that opens the inbox also shows the number of new messages. This method may change the dynamics. You can use the partialLinkTest () method to locate the link text of the uniform standard or known parts.

WebElement inboxLink = driver.findElement (By.partialLinkTest ("Inbox"))

System.out.println (inboxLink.getTest ())

The linkTest () and partialLinkTest () methods match all the links in this particular document and return the result value. In addition, if the developer provides the id,name,class property, we can also locate the links in these properties.

Note: when the positioning application exists in multiple modules, using text positioning elements will cause problems, and using parameterized text locators can also work.

(3) use the tagName () method to locate the element

Review: locate a single element using the findElement () method

Locate multiple elements using the findElements () method

Selenium webdriver's By class also provides tagName () to locate elements through its HTML tagName, which is similar to the getElementsByTagName () method in JS. It is convenient to use tagname to locate elements, such as locating all elements in tags.

Eg:

Assuming that there is a single button on the page, we can locate the button by using the following method

WebElement loginButton = driver.findElement (By.tagName ("button"))

LoginButton.click ()

The following example shows how many lines there are in the table tag

WebElement table = driver.findElement (By.id ("summerTable"))

List rows = table.findElement (By.tagName ("tr"))

AssertEquals (10 dint rows.size ())

The tagName () method locates the element through a DOM query and returns a list that matches the special tagname. This method may not be trusted when locating a single element or when there are instances of hungry elements on the page.

(4) use CSS selector to locate elements

Compared with Xpaths, this method is faster and more reliable.

In this section, we will explore some basic CSS selectors, and then we will delve into advanced CSS selectors later.

Using the css selector, the By class of selenium webdriver provides the cssSelector () method to locate the element

Absolute path to find elements

Considering the structural hierarchy of the element, the CSS absolute path references the very specific location of the element

The following example is to locate the area entered by the user name through the absolute path, giving space between elements while providing the absolute path.

WebElement userName = driver.findElement (By.sccSelector ("html body div div form input"))

You can also use the previous selector.

WebElement userName = driver.findElement ("html > body > div > div > form > input")

But this method has some limitations: it depends on the hierarchical structure of the page elements, and if the hierarchical structure changes, the positioning fails.

Locate elements by relative paths

We can locate the element directly through the relative path, independent of its position in the DOM.

For example, we can locate the area where the user name is entered in the following way, assuming that this element is in the first DOM file

WebElement userName = driver.findElemet (By.cssSelect ("input"))

The following is that the css selector locates the element using the Class and Id attributes through the relative path method, along with className () and id (). In addition to this, we can locate other attributes of the element in other ways that are not included in the By class.

Locate the element with the class selector

While using the CSS selector to locate the element, we can also use the attributes of the class to locate the element, which can be done using the defined HTML tag, and then add a point after the value of the class attribute

WebElement userName = driver.findElemet (By.cssSelect ("input.login"))

This method locates the input tag of the login button, whose attribute value is login.

Another way to abbreviate is to use one. By adding the attribute value, you can ignore the html tag, but this will return all elements with login classes, and the result may not be correct

WebElement loginButton = driver.findElement (By.cssSelect (.login))

Locate the element with the id selector

We can use the id assigned to the element to locate the element, which can be done with a html tag of the specified type, and then enter a hash followed by the value of the class attribute.

WebElement userName = driver.findElement (By.cssElement ("input#username"))

There is also an abbreviated form with the same shortcomings as above.

WebElement userName = driver.findElement (By.cssElement ("# username"))

The method of positioning with id

Locate the element with the attribute selector

Locate the element with the attribute selector

In addition to the class and id attributes, the css selector can also locate an element with other attributes of an element. The following example uses the name attribute to locate an element

WebElement userName = driver.findElement (By.cssElement ("input [name = username]"))

Use the name attribute to locate an element similar to the name () method under the By class

Locate the element with other attributes, such as using the alt attribute

element

WebElement userName = driver.findElement (By.cssElement ("IMG [SLT = Previous]"))

In such a case, an attribute may not be able to accurately locate the element, and it needs to be combined with additional attributes to accurately locate the element.

Eg: locate the element of the login button with multiple attributes

WebElement preciousButton = driver.find.Element (By.cssSelect ("input [type = 'sumbit'] [value='Login']"))

Locate the element with the attribute name selector

This method is different from the previous method, except that the previous positioning element is only based on the special attribute that the element is defined, not the attribute value, such as looking for all the

An element with an alt attribute in the element

List p_w_picpathsWithAlt = driver.findElement (By.cssSelect ("IMG [alt"))

The Brown not () pseudo class can also locate elements that do not meet the standard.

List p_w_picpathsWithAlt = driver.findElement (By.cssSelect ("img:not ([alt])"))

Perform partial matching of attribute values

Css provides a way to locate elements with partial matching of attribute values. This method is very effective for testing applications when attribute values are dynamically assigned and the page changes every time the page is requested. For example, ASP.NET 's id is dynamic, and the following table explains the syntax of the CSS partial matching method.

twenty-five

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 231

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report