Selenium Web Automation-how to find elements

Source: Internet
Author: User
Tags xpath jquery library

Selenium Web Automation-how to find elements


1. What is an element?


2. Positioning method Analysis

Selenium Webdriver provides an advanced technology to locate Web page elements. The Selenium feature-rich API provides multiple targeting strategies such as Name, ID, CSS selector, XPath, and so on, as shown in:

It is usually used to locate the ID, because it is unique, XPath is also more common, Firefox plugin: Firepath and Firebug are quick to give the XPath of the element.

In addition, jquery is a good way to locate

2.1 Positioning elements with jquery

jquery provides a Find method to quickly find page elements, but this approach is limited to pages that use the jquery library.

2.1.1 How to differentiate the page using jquery

Open Firefox, enter Google Address:, press F12 to bring up the console, then enter at the bottom of the arrow: "JQuery", and then enter:

    • If the console returns: "function (E, T)" This indicates that this page applies to the jquery library.
    • If the console returns an error message: Referenceerror:jquery is not defined this proves that the current page does not apply to the JQuery library

The code is as follows:

1  Packagewaytofindelement;2 3 Importjava.util.List;4 ImportOrg.openqa.selenium.JavascriptExecutor;5 ImportOrg.openqa.selenium.WebDriver;6 Importorg.openqa.selenium.WebElement;7 ImportOrg.openqa.selenium.firefox.FirefoxDriver;8 ImportOrg.testng.Assert;9 Importorg.openqa.selenium.WebDriverException;Ten  One  Public classFindbyjquery { A  -      Public Static voidMain (string[] args) { - Testnotloadjquery ();  the     } -  -     Private Static voidTestloadedjquery () { -Webdriver Driver =Newfirefoxdriver (); + driver.manage (). window (). Maximize (); -Javascriptexecutor Jsexecutor =(javascriptexecutor) driver; +Driver.get (""); A          atList<webelement> webelements = (list<webelement>) jsexecutor.executescript ("Return JQuery.find (' A.mnav ') )"); -Assert.assertequals (Webelements.size (), 6); -Assert.assertequals (Webelements.get (2). GetText (), "hao123");  - driver.quit (); -     } -      in     Private Static voidtestnotloadjquery () -     { toWebdriver Driver =Newfirefoxdriver (); + driver.manage (). window (). Maximize (); -Javascriptexecutor Jsexecutor =(javascriptexecutor) driver; theDriver.get (""); *         if(!jqueryloaded (Jsexecutor)) { $             //If no jquery library is detected, the injection operation is performedPanax Notoginseng injectjquery (jsexecutor); -         } the         //Find a Search button element +List<webelement> SearchButton = (list<webelement>) jsexecutor.executescript ("Return JQuery.find (' Input.sch_btn ') "); A         //Verify the text of the button theAssert.assertequals (Searchbutton.get (0). getattribute ("value"), "search"); + driver.quit (); -     } $      $     /**inject jquery support*/ -      Public Static voidinjectjquery (Javascriptexecutor jsexecutor) { -Jsexecutor.executescript ("var headid = document.getElementsByTagName (\" head\ ") [0];" the+ "var newscript = document.createelement (' script ');" -+ "Newscript.type = ' text/javascript ';"Wuyi+ "newscript.src=\"\ ";" the+ "Headid.appendchild (newscript);"); -     }     Wu      -      About     /**determine if the current page uses jquery*/ $      Public StaticBoolean jqueryloaded (javascriptexecutor jsexecutor) { -Boolean loaded =true; -         Try{ -Loaded = (Boolean) jsexecutor.executescript ("Return jQuery ()!" = NULL "); A}Catch(webdriverexception e) { +Loaded =false; the         } -          $         returnloaded; the     } the}
View Code

Selenium Web Automation-how to find elements

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: 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.