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!