[英]How do regular expressions work in selenium?
I want to store part of an id, and throw out the rest. 我想存储一部分id,然后扔掉剩下的部分。 For example, I have an html element with an id of 'element-12345'. 例如,我有一个id为'element-12345'的html元素。 I want to throw out 'element-' and keep '12345'. 我想扔出'element-'并保留'12345'。 How can I accomplish this? 我怎么能做到这一点?
I can capture and echo the value, like this: 我可以捕获并回显值,如下所示:
| storeAttribute | //pathToMyElement@id | myId | | echo | ${!-myId-!} | |
When I run the test, I get something like this: 当我运行测试时,我得到这样的东西:
| storeAttribute | //pathToMyElement@id | myId | | echo | ${myId} | element-12345 |
I'm recording with the Selenium IDE, and copying the test over into Fitnesse, using the Selenium Bridge fixture. 我正在使用Selenium IDE进行录制,并使用Selenium Bridge夹具将测试复制到Fitnesse。 The problem is I'm using a clean database each time I run the test, with random ids that I need to capture and use throughout my test. 问题是我每次运行测试时都使用干净的数据库,我需要在整个测试过程中捕获并使用随机ID。
The solution is to use the JavaScript replace()
function with storeEval
: 解决方案是使用带有storeEval
的JavaScript replace()
函数:
| storeAttribute | //pathToMyElement@id | elementID |
| storeEval | '${elementID}'.replace("element-", "") | myID |
Now if I echo myID
I get just the ID: 现在,如果我myID
我只得到ID:
| echo | ${myID} | 12345 |
/element-(\\d+)/i /元件 - (\\ d +)/ I
That's a regular expression that would capture the numbers after the dash. 这是一个正则表达式,可以捕获短划线后的数字。
Something like this might work: 像这样的东西可能会起作用:
| storeAttribute | fn:replace(//pathToMyElement@id,"^element-","") | myId |
To do regex requires XPath 2.0 - not sure which version Selenium implements. 要做正则表达式需要XPath 2.0 - 不确定Selenium实现的版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.