Selenium Webdriver XPath's positioning method practice!

Source: Internet
Author: User
Tags tagname xpath

HTML code:

Java code:
 Packagepage_elements_positioning;Importjava.util.List;Importorg.openqa.selenium.By;ImportOrg.openqa.selenium.WebDriver;Importorg.openqa.selenium.WebElement;ImportOrg.openqa.selenium.firefox.FirefoxDriver; Public classBy_tar_name { Public Static voidMain (string[] args) {String URL= "File:///C:/Users/ty/Desktop/selenium_test.html"; System.setproperty ("Webdriver.firefox.bin", "D:\\softerware\\firefox\\firefox\\firefox.exe");        Webdriver driver; Driver=NewFirefoxdriver ();        Driver.get (URL); Driver.findelement (By.tagname (A)); Webelement Link=driver.findelement (By.tagname ("a")); List<WebElement> links =driver.findelements (By.tagname ("a")); //Link.click ();System.out.println (links.get (0) + "--------" +links.get (1)); Links=driver.findelements (By.xpath ("//input[2]")); System.out.println ("2----------" +links.size ()); //1.: Absolute path---lookup;Links.addall (Driver.findelements (By.xpath ("/html/bodydiv/input[@value = ' query ']"))); System.out.println (Links.size ());//2.--using relative paths;Driver.findelement (By.xpath ("//input[2]") . Click ();//3. Use the property value of the page to locate the primitive instance;Webelement img=driver.findelement (By.xpath ("//img[@alt = ' div1-img1 ']")); //position the first image://mg[@href = ' www.sogou.com ')//Locate the value of input in the second div;//div[@name = ' div2 ']/input[@name = ' div2input '); //define the first link in the first Div//div[@id = ' Div1 ']/a[@herf = ' www.sogou.com ')//Locate the Query button on the page;//input[@type = ' button ')                                    }}

Selenium Webdriver XPath's positioning method practice!

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.