Locate the link list<webelement> startlink = driver.findelements (By.xpath ("//a[starts-with (@href, ') beginning with" http://v " Http://v ')]);
The location ID contains "i" link list<webelement> containlink = driver.findelements (By.xpath ("//a[contains (@id, ' I ')]");
Many documents have End-with API, but I use this function hint for invalid, not yet tested successfully, so do not write.
Summarize:
Package demo.test; Import java.util.List; Import Org.openqa.selenium.By; Import Org.openqa.selenium.WebDriver; Import org.openqa.selenium.WebElement; Import Org.openqa.selenium.firefox.FirefoxDriver; Import Org.testng.annotations.BeforeClass; Import Org.testng.annotations.Test; Import Com.framework.webdriver.DriverFactory; public class Findbyxpath {Webdriver driver, @BeforeClass public void Beforeclass () {this.driver = new firefoxdriver (); Driver.get ("http://www.baidu.com/"); } @Test public void Getelementbyxpath () {try {//Locates link list<webelement> startlink with "http://v" = Driver.findel Ements (By.xpath ("//a[starts-with (@href, ' http://v ')")); System.out.println ("startlinksize =" +startlink.size ()); The location ID contains "i" link list<webelement> containlink = driver.findelements (By.xpath ("//a[contains (@id, ' I ')]"); System.out.println ("containlinksize =" +containlink.size ()); } catch (Exception e) {System.out.println (e);}} }
Operation Result: