"Selenium 3+java Automation (5)"-xpath positioning

Source: Internet
Author: User

 Packagecom.mypro.jase;ImportJava.net.URL;Importjava.security.cert.PKIXRevocationChecker.Option;ImportJava.util.concurrent.TimeUnit;ImportJavax.xml.bind.annotation.XmlID;Importorg.openqa.selenium.By;ImportOrg.openqa.selenium.WebDriver;Importorg.openqa.selenium.WebElement;ImportOrg.openqa.selenium.firefox.FirefoxDriver; Public classFindbyxpath { Public Static voidMain (string[] args) {Webdriver driver=NewFirefoxdriver (); Driver.get ("Http://www.baidu.com");                Driver.manage (). window (). Maximize (); //One, the XPath commonly used localization method/XPath locates webelement element with id attribute= Driver.findelement (By.xpath ("//*[@id = ' kw ']")); Driver.manage (). Timeouts (). Implicitlywait (10, Timeunit.seconds); Element.sendkeys ("XPath is positioned by id attribute"); //XPath is positioned by the Name propertywebelement element = Driver.findelement (By.xpath ("//*[@name = ' WD ']")); Driver.manage (). Timeouts (). Implicitlywait (5, Timeunit.seconds); Element.sendkeys ("XPath is positioned by the Name property"); //XPath is positioned by the class attributewebelement element = Driver.findelement (By.xpath ("//*[@class = ' s_ipt ']")); Driver.manage (). Timeouts (). Implicitlywait (5, Timeunit.seconds); Element.sendkeys ("XPath is positioned by class attribute"); //Second, XPath: other attributes//XPath is positioned with other attributeswebelement element = Driver.findelement (By.xpath ("//*[@autocomplete = ' off ']")); Driver.manage (). Timeouts (). Implicitlywait (5, Timeunit.seconds); Element.sendkeys ("XPath is positioned by other attributes"); //Third, XPath: Label//XPath is positioned by tag: property is IDwebelement element = Driver.findelement (By.xpath ("//input[@id = ' kw ']")); Driver.manage (). Timeouts (). Implicitlywait (10, Timeunit.seconds); Element.sendkeys ("XPath is positioned by tag: property is ID"); //XPath is positioned by tag: property is namewebelement element = Driver.findelement (By.xpath ("//input[@name = ' WD ']")); Driver.manage (). Timeouts (). Implicitlywait (5, Timeunit.seconds); Element.sendkeys ("XPath is positioned by tag: property is Name"); //XPath is positioned by tag: property is Classwebelement element = Driver.findelement (By.xpath ("//input[@class = ' s_ipt ']")); Driver.manage (). Timeouts (). Implicitlywait (5, Timeunit.seconds); Element.sendkeys ("XPath is positioned by tag: property is Class"); //XPath is positioned by tag: other propertieswebelement element = Driver.findelement (By.xpath ("//input[@autocomplete = ' off ']")); Driver.manage (). Timeouts (). Implicitlywait (5, Timeunit.seconds); Element.sendkeys ("XPath is positioned by tag: other Properties"); //iv. XPath: Hierarchy//Locate the input field by locating its grandfather.webelement element = Driver.findelement (By.xpath ("//form[@id = ' form ']/span/input")); Driver.manage (). Timeouts (). Implicitlywait (5, Timeunit.seconds); Element.sendkeys ("XPath locates input input box by locating its grandfather."); //v. XPath: Index//The index of selenium is starting from 1.String Url2 = "https://passport.cnblogs.com/user/signin?ReturnUrl=http://home.cnblogs.com/set/profile/";        Driver.get (URL2);        Driver.manage (). window (). Maximize (); Driver.manage (). Timeouts (). Implicitlywait (5, Timeunit.seconds); Webelement name= Driver.findelement (By.id ("INPUT1")); Name.sendkeys ("[Email protected]"); Webelement PassWord= Driver.findelement (By.id ("Input2")); Password.sendkeys ("[Email protected]"); Webelement Login= Driver.findelement (By.id ("signin"));        Login.click (); Driver.manage (). Timeouts (). Implicitlywait (5, Timeunit.seconds); Webelement option= Driver.findelement (By.xpath ("//select[@id = ' s1 ']/option[2]"));                Option.click (); //vi. XPath: logical Operation//1.XPath also has a relatively strong function, can be multiple attribute logical operation, can be supported with (and), or (or), non (not)//2. The more commonly used is the and operation, while satisfying two propertieswebelement element = Driver.findelement (By.xpath ("//*[@id = ' kw ' and @autocomplete = ' off ']")); Element.sendkeys ("Logical Operation"); //vii. XPath: Fuzzy matching//XPath blur matches an attribute: @namewebelement element = Driver.findelement (By.xpath ("//*[contains (@name, ' hao123 ')]"));                Element.click (); //XPath fuzzy matchingwebelement element = Driver.findelement (By.xpath ("//*[contains" (Text (), ' hao123 ')] ")); Element.click ();*///XPath Blur match: what starts withwebelement element = Driver.findelement (By.xpath ("//*[starts-with (@id, ' s_kw_ ')]"));                Element.clear (); //XPath blur match: what resultswebelement element = Driver.findelement (By.xpath ("//*[ends-with (@class, ' quickdelete-wrap ')]/input")); Element.sendkeys ("1243243"); webelement element= Driver.findelement (By.xpath ("//*[matchs () (" Text (), ' hao13 ')] "));    Element.click (); }}

"Selenium 3+java Automation (5)"-xpath positioning

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.