Xpath: Use partial attribute value fuzzy matching to locate page elements

Source: Internet
Author: User
Tags xpath testng

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:


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.