Search code examples
javaxpathhtmlunit

Does this xpath look correct?


page.getByXPath("//*[@href='http://www.example.com/index.do/abc/1_*'");

Do I need to escape any characters?

I am trying to get all ahref links on the page that have the pattern of:

http://www.example.com/index.do/abc/1_

so these should all be retrieved:

http://www.example.com/index.do/abc/1_asdf-asdfasdf
http://www.example.com/index.do/abc/1_223
http://www.example.com/index.do/abc/1_as.php
http://www.example.com/index.do/abc/1_2222233

Solution

  • There are no wildcards in XPath. You want something like this instead:

    page.getByXPath("//*[contains(@href,'http://www.example.com/index.do/abc/1_')]");
    

    This relies on the contains function. You can also use the starts-with function:

    //*[starts-with(@href,'http://www.example.com/index.do/abc/1_')]