regex - 如何仅捕获 id 的一部分?

标签 regex xpath selenium fitnesse selenium-fitnesse-bridge

我正在 try catch 将随机生成的元素的 id。我可以像这样成功地捕获我的元素 id 的值......

| storeAttribute | //div[1]@id | variableName |

现在我的变量将类似于...
divElement-12345

我想删除'divElement-',这样我留下的变量是'12345',以便我以后可以使用它来选择与之关联的'form-12345'元素......像这样:
| type | //tr[@id='form-${variableName}']/td/form/fieldset/p[1]/input | Type this |

我怎么能做到这一点?

最佳答案

Selenium 中有两个选项,XPath 和 CSS 选择器。我读过 CSS Selector 更适合在 FireFox 和 IE 中进行测试。
使用最新版本的 Selenium IDE(2009 年 3 月 5 日),我成功地使用了评估 Javascript 表达式的 storeEval,让您可以访问 javascript 字符串函数。

XPath:

storeAttribute | //div[1]@id                                            | divID
storeEval      | '${divID}'.replace("divElement-", "")                  | number
type           | //tr[@id='form-${number}']/td/form/fieldset/p[1]/input | Type this

CSS 选择器:
storeAttribute | css=div[1]@id                                                     | divID
storeEval      | '${divID}'.replace("divElement-", "")                             | number
type           | css=tr[id='form-${number}'] > td > form > fieldset > p[1] > input | Type this

关于regex - 如何仅捕获 id 的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/401905/

相关文章:

python - 使用 Selenium 进行网页抓取

使用 URL 检查正则表达式的 JavaScript URL 正则表达式

python - 用于匹配名称后跟可选空格的正则表达式,但忽略注释

xml - 使用 XSLT 从带有嵌入链接的 XML 中提取纯文本

xml - XML 属性的跟随兄弟

javascript - XML:通过值而不是属性来选择节点

python - Selenium ChromeDriver 找不到元素,而 firefox 驱动程序可以找到相同的元素(Python)

Python:从 tar.gz 中提取具有模式的特定文件而不提取完整文件

python - re.findall 表现得很奇怪

java - Appium Webdriver 导入问题