如何在硒定位器中使用正则表达式

问题描述 投票:11回答:5

我正在使用硒RC,例如,我希望获得所有具有属性href匹配的链接元素:

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

我想使用:

sel.get_attribute( '//a[regx:match(@href, "http://[^/]*\d+.com")]/@name' )

这将返回与正则表达式匹配的所有链接的名称属性的列表。(或类似的内容)

谢谢

regex xpath selenium selenium-rc
5个回答
12
投票

上面的答案可能是找到所有与正则表达式匹配的链接的正确方法,但是我认为回答问题的另一部分(如何在Xpath定位器中使用正则表达式也很有帮助)。您需要使用正则表达式matches()函数,如下所示:

xpath=//div[matches(@id,'che.*boxes')]

(当然,这会单击带有'id = checkboxes'或'id = cheANYTHINGHEREboxes'的div)

但是请注意,Xpath的所有本机浏览器实现都不支持matchs函数(最明显的是,在FF3中使用此函数将引发错误:无效的xpath [2]。]

[如果您在使用特定的浏览器时遇到麻烦(就像我在FF3上所做的那样,请尝试使用Selenium的allowNativeXpath(“ false”)切换到JavaScript Xpath解释器。它会慢一些,但似乎可以与更多的Xpath函数一起使用,包括“ matches”和“ ends-with”。 :)


3
投票

您可以使用Selenium命令getAllLinks来获取页面上链接ID的数组,然后可以循环浏览并使用getAttribute检查href,该getAttribute使用定位符,后跟@和属性名称。例如,在Java中,可能是:

String[] allLinks = session().getAllLinks();
List<String> matchingLinks = new ArrayList<String>();

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

1
投票

一种可能的解决方案是使用sel.get_eval()并编写一个返回链接列表的JS脚本。类似于以下答案:selenium: Is it possible to use the regexp in selenium locators


0
投票

还有Selenium RC的一些替代方法。这些不是纯粹的Selenium解决方案,它们允许与您的编程语言数据结构和Selenium进行交互。

您还可以获取HTML页面源,然后使用正则表达式源返回匹配的一组链接。使用正则表达式分组可以分离出URL,链接文本/ ID等,然后您可以将其传递回硒以单击或导航到。

[另一种方法是获取HTML页面源或父/根元素的innerHTML(通过DOM定位器),然后将HTML转换为XML,成为编程语言中的DOM对象。然后,您可以使用所需的XPath(是否带有正则表达式)遍历DOM,并获得仅包含感兴趣链接的节点集。从他们解析出来的链接文本/ ID或URL,然后您可以返回到硒以单击或导航到。]

根据要求,我在下面提供示例。由于帖子似乎不是特定于语言的,因此使用的语言多种多样。我只是使用我现有的可一起破解的示例。它们尚未经过完全测试或测试,但是我之前在其他项目中已经使用过部分代码,因此这些都是概念证明的代码示例,这些示例说明了如何实现我刚刚提到的解决方案。

//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("/<a.+href=\"(.+)\"/",$pgSrc,$matches,PREG_PATTERN_ORDER);
//$matches is a 2D array, $matches[0] is array of whole string matched, $matches[1] is array of what's in parenthesis
//you either get an array of all matched link URL values in parenthesis capture group or an empty array
$links = count($matches) >= 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
*/

0
投票

Selenium的By.Id和By.CssSelector方法不支持Regex,而By.XPath仅在启用XPath 2.0的情况下才支持。如果要使用正则表达式,可以执行以下操作:

© www.soinside.com 2019 - 2024. All rights reserved.