How to use regex in selenium locators

前端 未结 5 1811
陌清茗
陌清茗 2020-12-14 21:49

I\'m using selenium RC and I would like, for example, to get all the links elements with attribute href that match:

http://[^/]*\\d+com

I w

5条回答
  •  粉色の甜心
    2020-12-14 22:41

    You can use the Selenium command getAllLinks to get an array of the ids of links on the page, which you could then loop through and check the href using the getAttribute, which takes the locator followed by an @ and the attribute name. For example in Java this might be:

    String[] allLinks = session().getAllLinks();
    List matchingLinks = new ArrayList();
    
    for (String linkId : allLinks) {
        String linkHref = selenium.getAttribute("id=" + linkId + "@href");
        if (linkHref.matches("http://[^/]*\\d+.com")) {
            matchingLinks.add(link);
        }
    }
    

提交回复
热议问题