Locate the XPath details of the page element and frequently asked questions that do not locate the test element

Source: Internet
Author: User

First, the method of locating elements

    • ID: Preferred recognition attribute, the standard recommended for each element of the page set a unique id attribute,

If it is not and it is difficult to find the unique attribute, workaround: (1) Find the development to add the ID or name. If not, the solution can be:

1. Find the feature of the button, such as the text of the button is submit;

2. Using XPath to locate, you can write://button[@value = ' Submit ']. (Note is single quote!!) )

  • name: Same as ID
  • ClassName: a good way to clean sweep some elements with the same class
  • link text and partial link text: Used to locate hyperlinks on more
  • tag name: A bit similar to class name, but with multiple results, the multi-use list displays:

    The difference between findelement and findelements:

    Findelement returns an element object, otherwise throws an exception.

    Findelements returns an element list that meets the criteria, and returns an empty list if there are no eligible elements.

    View Code
  • CSS selector: familiar with jquery is easier to navigate to, visible http://saucelabs.com/resources/selenium/css-selectors. #为class,. For name
  • jquery: That is, using JS to perform query positioning, call the Javascriptexecutor class to execute the JQuery find () method to find elements, you can refer to

Bo Master http://www.cnblogs.com/tobecrazy/p/4817946.html written in quite detailed

    • XPath: You can use this method to not locate any of the above methods, but remember to optimize the path instead of ... /HTML/BODY/DIV/DIV[2]/DIV[2]/DIV[2]/DIV[5]/DIV/P[2] ... The performance is very poor good!

XPath Disadvantages:

1. Poor performance, the performance of the positioning element is worse than most other methods;

2. Not strong enough, XPath will change with the layout of the page elements, poor readability, can hardly maintain

XPath Benefits:

1. XPath can find its ancestor (ancestors) through an element ("/."). /"or" Ancestor-or-self::book ");

2. Boolean logic can be judged, e.g./button[@value = ' Submit ' or @name = ' Tijiao ')

Second, how to use XPath positioning:

The location path can be a relative path or an absolute path. The absolute positioning path begins with a slash "/" and the relative positioning path does not. such as:/div/input and Div/input

First method: Positioning by Absolute path

By.xpath ("Html/body/div/form/input")
By.xpath ("//input")
Second method: by Element Index
By.xpath ("//input[4]")
The third method: Use the XPath attribute to locate (combined with the 2nd, 3rd method can be used), preceded by:TagName [@ property = ' property value '] represents
By.xpath ("//input[@id = ' kw1 ']") 
By.xpath ("//input[@name = ' kw1 ']") 
Fourth method: Use partial attribute value matching (the most powerful method)
By.xpath ("//input[start-with (@id,' nice ')  
By.xpath ("//input[ends-with (@id, ' very pretty")
By.xpath ("//input[contains (@id, ' so Beautiful ')]")
Three, positioning the page element common problems
1. Element display has ID, but is not located (do not know why, consult a group owner said the positioning principle is not the same), such as name,xpath,!!!!
2. No wait time is set, the operation is performed before the element is found, resulting in an error, the workaround:
(1) Display wait: It is clear to wait for an element to appear or an element of the clickable conditions, etc., and so on, and so on, unless not found within the specified time, then jump out of exception
New Webdriverwait (Driver). Until (expectedconditions.presenceofelementlocated (    by.cssselector ("CSS Locator ")));

Or you can write that.

 webdriver Driver = new   Firefoxdriver ();d river.get (http:  // Span style= "color: #008000;" >somedomain/url_that_delays_loading);  Webelement e = (new  webdriverwait (driver, 10 new  expectedcondition< webelement> () {//  Wait 10 seconds until the ID element is found   @Override  Span style= "color: #0000ff;" >public   Webelement apply (Webdriver d) { return  d.findelement (by.id ("ID locator" )); }    });
Implicitly waits for Driver.manage (). Timeouts (). implicitlywait (1, timeunit.seconds);
But we can also put it in a small package:
 public  boolean  isbyelementdisplayed (by by, int   time) { boolean  status = false  ;  if  (Driver.findelement (by). isdisplayed () = = false      else   {status  = true  ;  return   status;}   

(3) Thread hibernation: Thread.Sleep (3000);

3. Dynamic ID, it is recommended to use the relative path of XPath to locate

4. Input box and button combination, input box input after no loss of focus, level coverage, resulting in the button can not be found, set wait to!!!!!!

5.iframe frame, Time control and map selection control follow-up introduction!!!

6. Rich Text edit box, embedded HTML, such as Ueeditor, can use JS, absolutely works

      String text = "Hello";              = "document.getElementById (' Ueditor_0 '). Contentdocument.write ('" + text + "');" ;             ((Javascriptexecutor) driver). Executescript (JS);

7. The most worrying is the modal window does not know what a ghost. The first is not a new window, can not use windowshandle positioning, there is no IFRAME frame, and also does not belong to the popup box, can not be used SwitchTo to deal with, so this need to consult the great God Understanding.

The basic understanding of this is it!



Locate the XPath details of the page element and frequently asked questions that do not locate the test element

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.