Java Selenium webdriver Actual combat page element positioning

Source: Internet
Author: User
Tags tag name tagname xpath

During the implementation of automated testing, there are three steps to common page operations in the test program

1. Locate the page elements on the page and store them in a variable

2. Manipulate the page elements stored in the variable, click, drop down, or enter text, etc.

3. Set the action value of the page element, for example, select the drop-down list in the drop-down list or enter what value in the input box

The positioning of the page element is the first step in the three steps, this article describes the commonly used positioning methods

The Findelement function of the Webdriver object is used to locate a page element, findelements function The user locates multiple page elements, and the positioned page elements are stored using Webelement objects

The common methods are:

1. Use ID to locate

Driver.findelement (by.id ("id value"));
webelement element = Driver.findelement (By.id ("username"));

2. Use name to locate

Driver.findelement (By.name ("Name Value"));
webelement element = Driver.findelement (By.name ("username"));

3. Use the entire text location of the link

Driver.findelement (By.linktext ("Full text of the link"));
Driver.findelement (By.linktext ("My Project"));

4. Use partial link text targeting

Driver.findelement (By.partiallinktext ("part of the linked text content"));
Driver.findelement (By.partiallinktext ("project"));

5. Use XPath to position

XPATH is a language for finding information in an XML document, which is the abbreviation for XML path. XPATH can be used in XML traversal of elements and attributes in the document provides the ability to browse the tree

This method is a very powerful way to find elements that can be used to locate almost any element on a page.

The browser has the ability to export XPath from HTML

The XPath exported here is://*[@id = "Lst-ib")

1.xPath Absolute Positioning

Find a div with a value of button

Webelement button = driver.findelement (By.xpath ("/html/body/div/input[@value = ' query ']");

Using an absolute path is very fragile, because even if you make a small change in your code, it can cause the original XPath expression to fail to locate

2. Using relative path positioning

Webelement button = driver.findelement (By.xpath ("//div/input[@value = ' query ']");

Practical Examples:

Query the second input,xpath is starting from 1

Driver.findelement (By.xpath ("//input[2]"));

Position the first picture of the page

Driver.findelement (By.xpath ("//img[@href = ' xxx ')");

Position the first input input box in the second Div

Driver.findelement (By.xpath ("(//div[@class = ' mob-share-list ')" [2]/input[1] "));

Fuzzy positioning: Start-with

Look for the A element with the Rel attribute starting with Ahaha

Driver.findelement (By.xpath ("//a[starts-with (@rel, ' Ahaha ')"));

Fuzzy positioning: Contains

Need to find content containing the button to submit the word

Driver.findelement (By.xpath ("//button[contains (Text (), ' submit ')]);

Find the Rel attribute contains the A element of Ahaha

Driver.findelement (By.xpath ("//a[contains (@rel, ' Ahaha ')"));

5. Using CSS to position

Driver.findelement (By.cssselector ("CSS positioning expression"));d river.findelement (By.cssselector ("img.plusimg")); Driver.findelement (By.cssselector ("input[type= ' button ')");d river.findelement (By.cssselector (" Input#div1input ")); // based on the attribute value of the ID driver.findelement (by.cssselector ("img[alt= ' img1 '][href= ' http://www.sougou.com ']");

6. Use the class name to locate

Driver.findelement (By.classname ("Page's class attribute value"));
Driver.findelement (By.classname ("tight left");//To write full

7. Use tag name to locate

Driver.findelement (by.tagname ("HTML tag name of the page"));
webelement element = Driver.findelement (By.tagname ("a"));
list<webelement> elements = driver.findelements (By.tagname ("a"));

8. Using the JS expression

((Javascriptexecutor) driver). Executescript ("JS expression"= (javascriptexecutor) driver; Js1.executescript ( "Document.getelementsbyclassname (\" ke-edit-iframe\ ") [0].setattribute (\" name\ ", \" frame-name\ ");

Java Selenium webdriver Actual combat page element positioning

Related Article

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.