How to use regex in selenium locators

前端 未结 5 1809
陌清茗
陌清茗 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:44

    Here's some alternate methods as well for Selenium RC. These aren't pure Selenium solutions, they allow interaction with your programming language data structures and Selenium.

    You can also get get HTML page source, then regular expression the source to return a match set of links. Use regex grouping to separate out URLs, link text/ID, etc. and you can then pass them back to selenium to click on or navigate to.

    Another method is get HTML page source or innerHTML (via DOM locators) of a parent/root element then convert the HTML to XML as DOM object in your programming language. You can then traverse the DOM with desired XPath (with regular expression or not), and obtain a nodeset of only the links of interest. From their parse out the link text/ID or URL and you can pass back to selenium to click on or navigate to.

    Upon request, I'm providing examples below. It's mixed languages since the post didn't appear to be language specific anyways. I'm just using what I had available to hack together for examples. They aren't fully tested or tested at all, but I've worked with bits of the code before in other projects, so these are proof of concept code examples of how you'd implement the solutions I just mentioned.

    //Example of element attribute processing by page source and regex (in PHP)
    $pgSrc = $sel->getPageSource();
    //simple hyperlink extraction via regex below, replace with better regex pattern as desired
    preg_match_all("/= 2 ? $matches[1] : array();
    //now do as you wish, iterating over all link URLs
    //NOTE: these are URLs only, not actual hyperlink elements
    
    //Example of XML DOM parsing with Selenium RC (in Java)
    String locator = "id=someElement";
    String htmlSrcSubset = sel.getEval("this.browserbot.findElement(\""+locator+"\").innerHTML");
    //using JSoup XML parser library for Java, see jsoup.org
    Document doc = Jsoup.parse(htmlSrcSubset);
    /* once you have this document object, can then manipulate & traverse
    it as an XML/HTML node tree. I'm not going to go into details on this
    as you'd need to know XML DOM traversal and XPath (not just for finding locators).
    But this tutorial URL will give you some ideas:
    
    http://jsoup.org/cookbook/extracting-data/dom-navigation
    
    the example there seems to indicate first getting the element/node defined
    by content tag within the "document" or source, then from there get all
    hyperlink elements/nodes and then traverse that as a list/array, doing
    whatever you want with an object oriented approach for each element in
    the array. Each element is an XML node with properties. If you study it,
    you'd find this approach gives you the power/access that WebDriver/Selenium 2
    now gives you with WebElements but the example here is what you can do in
    Selenium RC to get similar WebElement kind of capability
    */
    

提交回复
热议问题