Java Selenium element Positioning encyclopedia _java

Source: Internet
Author: User
Tags html sample tagname unique id xpath

Page element positioning is the most important thing in automation, selenium Webdriver provides a number of ways to locate elements. Testers should have a good command of various positioning methods. Use the simplest and most stable positioning method.

Reading Table of Contents

    1. Automated test steps
    2. Positioning Method Encyclopedia
    3. How to locate
    4. Find elements by ID: by.id ()
    5. Find element by name: By.name ()
    6. Find elements by TagName: By.tagname ()
    7. Find elements by ClassName By.classname
    8. Find element By.linktext () by LinkText;
    9. Find element By.partiallinktext () by Partiallinktext
    10. Find elements through CSS selectors,
    11. Finding elements through XPath

Automated test steps

In the process of automated testing, the test program usually operates page element steps

1. Find the page element of the Web and assign it to a storage object (webelement)

2. To store the page elements of the object to operate, such as: Click on the link, enter characters in the input box, etc.

3. Verify that the elements on the page meet expectations

With these three steps, we can complete the operation of a page element, and finding the page element is an important step. I can't find the page element, I can't do it later.

The real complexity of web page technology makes a lot of page elements difficult to locate. People often don't know how to locate.

Positioning Method Encyclopedia

Define a Web page element by using the Findelement function of the Webdriver object

Use the Findelements function to locate multiple elements of a page

Positioned page elements need to be stored using the Webelement object for subsequent use

The commonly used positioning page element method is as follows, sorted by recommendation

Positioning method

Java language Implementation Instance

ID Location

Driver.findelement (By.id ("Value of ID"));

Name positioning

Driver.findelement (By.name ("Value of Name"));

All text positioning of links

Driver.findelement (By.linktext ("All text of the link"));

Partial text positioning for links

Driver.findelement (By.partiallinktext ("Part of the link text"));

CSS style positioning

Driver.findelement (by.cssselector ("CSS expression"));

XPath way to locate

Driver.findelement (By.xpath ("XPath expression"));

Class name Positioning

Driver.findelement (By.classname ("Class attribute"));

TagName Label name positioning

Driver.findelement (By.tagname ("label name"));

jquery Way

Js.executescript ("Return jquery.find (" jQuery expression) ")

How to locate

When you use selenium webdriver for element positioning, you typically use the Findelement or Findelements method to return an element handle with the by class to locate the element

The Findelement () method returns an element that, if not found, throws an exception noelementfindexception ()

The Findelements () method returns a number of elements that, if not found, return an empty array and do not throw an exception

How to choose a positioning method

The strategy is to choose a simple, stable positioning method.

1. When the page element has id attribute, try to use ID to locate. If not, then choose another location method

2. Cssselector execution speed, recommended use

3. When positioning hyperlinks, you can consider Linktext or partiallinktext: But note that the text changes frequently, so it is not recommended

4. XPath is the most powerful.  The execution was slow because it was necessary to find the entire DOM, so try to use less. When there's no way to use XPath

Find elements by ID: by.id ()

Finding elements through the ID of the page element is the most recommended way, and the Consortium recommends that developers provide unique ID attributes for each page element

Once the element is given a unique id attribute, it is easy to navigate to the element when we do automated testing. The ID of the element is used as the preferred recognition attribute because it is the fastest recognition policy.

Take the Baidu homepage as an example, the HTML sample code for the search box is as follows, and its ID is kw

<input type= "text" autocomplete= "Off" maxlength= "M" id= "kw" Name= "wd" class= "S_ipt" >

"Baidu Click" the HTML sample code for the search button element is as follows, and its ID is SU

<input type= "Submit" class= "BTN self-btn bg s_btn" id= "su" value= "Baidu click" >

The Java sample code to find elements by ID in Selenium/webdriver is as follows

Webdriver Driver = new Firefoxdriver ();
  Driver.get ("http://www.baidu.com");
   
  Webelement searchbox = driver.findelement (by.id ("kw"));
  Searchbox.sendkeys ("Small Tank Blog Park");
  Webelement SearchButton = driver.findelement (by.id ("su"));
  Searchbutton.submit ();
  
  Driver.close ();

Find element by name: By.name ()

Take the Douban homepage search box As an example, the HTML code for its search box is as follows, and its name is: Q

<input type= "text" autocomplete= "Off" name= "Q" placeholder= "book, film, music, group, station, member" size= "" Maxlength= "" >

Webdriver the Java code for the search box on the home page of watercress by name is as follows:

 Webdriver Driver = new Firefoxdriver ();
  Driver.get ("http://www.douban.com");
   
  Webelement searchbox = driver.findelement (By.name ("Q"));
  Searchbox.sendkeys ("Small Tank");
  Searchbox.submit ();
 

Find elements by TagName: By.tagname ()

When searching for elements through tagname, multiple elements are returned. So you need to use Findelements ()

 Webdriver Driver = new Firefoxdriver ();
  Driver.get ("http://www.cnblogs.com");
   
  list<webelement> buttons = driver.findelements (By.tagname ("div"));
  System.out.println ("button:" + buttons.size ());

Note: If you use tagname, be aware that the tagname of many HTML elements is the same,

such as a radio box, check box, text box, password box. These element tags are input. At this point, tagname cannot get exactly what we want, and we need to combine the type attribute to filter out the elements we want.

Webdriver Driver = new Firefoxdriver ();
  Driver.get ("http://www.cnblogs.com");
   
  list<webelement> buttons = driver.findelements (By.tagname ("input"));
  for (Webelement webelement:buttons) {
   if (Webelement.getattribute ("type"). Equals ("text")) {
    SYSTEM.OUT.PRINTLN ("Input text is:" + webelement.gettext ());
   }
  

Find elements by ClassName By.classname

To Taobao's homepage search for example, the HTML code for its search box is as follows: class= "Search-combobox-input"

<input autocomplete= "Off" autofocus= "true" accesskey= "s" aria-label= "Please enter search text" name= "q" id= "Q" class= " Search-combobox-input "aria-haspopup=" true "aria-combobox=" List "role=" ComboBox "X-webkit-grammar=" BUILTIN: Translate "tabindex=" 0 ">

The Java sample code is as follows

  Webdriver Driver = new Firefoxdriver ();
  Driver.get ("http://www.taobao.com");
  Thread.Sleep (15000);
  Webelement searchbox = driver.findelement (By.classname ("Search-combobox-input"));

  Searchbox.sendkeys ("Down jacket");
  Searchbox.submit ();

Note: When you use classname for element positioning, you sometimes encounter a

Find element By.linktext () by LinkText;

Position elements directly through text information on hyperlinks: for example

<a href= "Https://passport.baidu.com/v2/?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F" name= "Tj_login" class= "lb" onclick= "return false;" > Login </a>
 

The HTML code is as follows

 Webdriver Driver = new Firefoxdriver ();
  Driver.get ("http://www.baidu.com");
  Webelement Loginlink = driver.findelement (By.linktext ("login"));
  Loginlink.click ();

Find element By.partiallinktext () by Partiallinktext

This method is a stronger version of the previous method, and you can use this method to locate the element by using some hyperlink text only when you want to match it with some keywords.

The HTML code is as follows

  Webdriver Driver = new Firefoxdriver ();
  Driver.get ("http://www.baidu.com");
  Webelement Loginlink = driver.findelement (By.partiallinktext ("Deng"));
  Loginlink.click ();

Note: When positioning in this way, the problem that may arise is that when you do not know that a hyperlink contains "et" in your page, the Findelement method returns only the first element found, not all eligible elements.

If you want to get all the eligible elements, you can only use the Findelements method.

The above is the Java Selenium element positioning data collation, follow-up continue to supplement the relevant information, thank you for your support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.