硒:是否可以在硒定位器中使用正则表达式 [英] selenium: Is it possible to use the regexp in selenium locators
本文介绍了硒:是否可以在硒定位器中使用正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获取所有具有与某些正则表达式匹配的文本的 div/links/.. 的 xpath 计数.例如:
I want to get the xpath count of all the divs/links/.. that have text matching some regular expression. For example:
<span> day 2 night </span>
<span> day 4 night </span>
<span> day 17 night</span>
我希望能够致电:
sel.get_xpath_count('regexp:day \d night')
并让它返回2.(当然这是一个简单的例子,我想使用各种正则表达式)
and have it return 2. (This is a simple example of course, I would like to use all kinds of regular expressions)
这可能吗?如何做到?
推荐答案
使用允许您使用 javascript 的 dom=
协议.并且 javascript 有正则表达式:
Use the dom=
protocol which allows you to use javascript. And javascript has regexp:
# sorry, example in Perl:
$sel->get(qq{dom=(function(){
var x = document.getElementsByTagName('span');
var result = [];
for (var i=0;i<x.length;i++) {
var txt = x[i].innerHTML;
if (txt.match(/day \d night/)) {
result.push(x[i]);
}
}
return result;
})()});
这篇关于硒:是否可以在硒定位器中使用正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文