Selenium using Notes (iii)--element positioning

Source: Internet
Author: User
Tags xpath email account

One of the most important things about selenium automated testing is the element positioning, which cannot be manipulated if the element is not positioned, and it is impossible to automate the test. Online for the element positioning has a lot of introduction, very detailed and detailed, but still have a lot of novice will always encounter can not locate the situation, here their own summary of the element positioning.

First look at the way element is positioned

          //use ID to locateUserName = Driver.findelement (By.id ("Tangram__psp_8__username")); //use class to locateUserName = Driver.findelement (By.classname ("Pass-text-input pass-text-input-username")); //use name to locateUserName = Driver.findelement (By.name ("UserName")); //using XPath to locate//Absolute PathUserName = Driver.findelement (By.xpath ("/html/body/div[6]/div[2]/div[2]/div/div/div/div/div/div[1]/form/p[5]/ Input)); //positioning using element attribute valuesUserName = Driver.findelement (By.xpath ("//*[@id = ' tangram__psp_8__username ']")); //or locate the parent level element FirstUserName = Driver.findelement (By.xpath ("//*[@id = ' tangram__psp_8__usernamewrapper ']/input")); //using logical operatorsUserName = Driver.findelement (By.xpath ("//*[@id = ' tangram__psp_8__username ' and @name = ' userName ']")); //contains contains a propertyUserName = Driver.findelement (By.xpath ("//input[contains (@id, ' Tangram__psp_8__username ')]");

Missing one and CSS positioning, generally in the project with XPath and CSS basically can locate all elements, but from the efficiency of the word or CSS is relatively high. Individuals prefer to use XPath positioning, ID, class, name, TagName, and so on these easy to locate the situation, and some elements of the property or dynamic changes, if using these positioning methods to locate the words prone to error. Take XPath for example, what should we do when we have an element that can't be positioned? I'll say my idea:

1. Look at the HTML, see if this element is wrapped in a framework such as IFRAME, if there is a framework, you need to switch frame, with 163 mailbox login as an example:

<HTMLstyle= "Display:block;"><Head><Bodyclass= "Move"style= "padding-top:0px;"><Headerclass= "header">< SectionID= "MAINBG"class= "Main"style= "Background-color:rgb (249, 249, 249);"><DivID= "maincnt"class= "Main-inner"style= "Background-image:url" ("http://mimg.127.net/m/lc/img/LoginBackgroundPic/1061/2/promPic.jpg ");"><DivID= "Loginblock"class= "Login Tab-2"><Divclass= "Loginfunc"><DivID= "Applogintab"class= "LoginForm"><DivID= "Normallogintab"class= "LoginForm"><Divclass= "Loginwrap"><DivID= "Logindiv"class= "Loginurs"style= "width:295px; height:413px;"><iframeID= "X-urs-iframe"frameborder= "0"name=""scrolling= "No"style= "width:100%; height:100%; border:medium none; Background:none Repeat scroll 0% 0% transparent;"src= "http://dl.reg.163.com/webzj/m163_1.0.1/pub/index_dl.html?wdaId="><!DOCTYPE HTML><HTML><Head><Body><DivID= "Confirm"class= "M-confirm f-dn"><DivID= "Cnt-box-parent"class= "G-BD cnt-box-include"><DivID= "Loading"class= "Loading F-dn"><DivID= "Cnt-box"class= "G-BD"><Divclass= "M-header"><Divclass= "m-cnt"><formID= "Login-form"><DivID= "auto-id-1482585786268"class= "M-container"><DivID= "Account-box"class= "InputBox"><Divclass= "U-logo"><DivID= "auto-id-1482585786267"class= "u-input box"><labelID= "auto-id-1482585786237"class= "U-label f-dn"style= "Display:block;">email account or phone number</label><inputID= "auto-id-1482585786223"class= "J-inputtext dlemail"type= "text"spellcheck= "false"TabIndex= "1"AutoComplete= "Off"data-required= "true"Data-type= "Email"name= "Email"Data-placeholder= "email account or phone number"placeholder= "email account or phone number"style= "WIDTH:122PX;">

You can see the account input box is in an IFRAME frame, we have to switch frames to locate the account input box, how to switch it, very simple, locate the IFRAME, and then switch

Webelement frame = dr.findelement (By.xpath ("//*[@id = ' x-urs-iframe ']");
Dr.switchto (). frame (frame);

2. If there is no framework, or can not locate, to pay attention to the reading of the wrong message, it is impossible to locate this element or the element is not visible. When we do automated testing, it is easy to have a misunderstanding, is to take the logic of their own to do an operation, and ignore the program itself on the processing of this logic. For example, when we log in, we need to click the login button, pop-up login window and then login. Some children's shoes directly locate the account password input box, automation, how simple how to come, which of course is not. When you write the script must pay attention to, manual how to point, script how to write, how step by step implementation, can not miss. If you can't navigate to an element, look for what you missed before locating the element.

3. Do not trust yourself to write the code when positioning. For example, I wrote a location below, how to check the code is not a problem is not located. Do not rush this time, first find out whether the problem is the element itself or the code of the problem, using another positioning method to determine whether the location of their own write a problem. I often see many children's shoes in the group stick their own code to say that no problem ah how is not located, and then the group of enthusiastic friends to him rewrite a positioning, he filled with joy of a try, ah can have nothing to do, do not find out from their own why not locate the reason.

UserName = Driver.findelement (By.xpath ("//*[@id = ' tangram__psp_8__usernamewrapper ']/input"));

4. When the element structure is more complex, learn to use the positioning method flexibly, such as multi-use of the inclusion of the positioning method can better solve the problem.

Contains contains a property

UserName = Driver.findelement (By.xpath ("//input[contains (@id, ' Tangram__psp_8__username ')]");

In short, the element positioning is not difficult, to practice more, not positioning themselves to solve, their own reflection is where the problem, so as to improve their own.

Selenium using Notes (iii)--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.