如何仅捕获 id 的一部分?

发布于 2024-07-11 04:29:29 字数 401 浏览 8 评论 0原文

我正在尝试捕获将随机生成的元素的 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 |

我如何才能完成此任务?

I'm trying to capture the id of an element that will be randomly generated. I can successfully capture the value of my element id like this...

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

Now my variable will be something like...

divElement-12345

I want to remove 'divElement-' so that the variable I am left with is '12345' so that I can use it later to select the 'form-12345' element associated with it...something like this:

| type | //tr[@id='form-${variableName}']/td/form/fieldset/p[1]/input | Type this |

How might I be able to accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笨死的猪 2024-07-18 04:29:30

XPATH 中有很多函数可以解决您的问题。 假设“divElement-”是一个不会改变的常量,并且您使用的是 XPath 2.0,我建议:

substring-after(div[1]/@id/text(),"divElement-")

There are many functions in XPATH which should solve your problem. Assuming "divElement-" is a constant that will not change and that you are using XPath 2.0, I would suggest:

substring-after(div[1]/@id/text(),"divElement-")

吖咩 2024-07-18 04:29:29

Selenium 有两个选项:XPath 和 CSS 选择器。 我读过 CSS 选择器更适合在 FireFox 和 IE 中进行测试。
使用最新版本的 Selenium IDE(2009 年 3 月 5 日),我成功地使用了 storeEval 来计算 Javascript 表达式,使您可以访问 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

You have two options in Selenium, XPath and CSS Selector. I have read that CSS Selector is better for doing tests in both FireFox and IE.
Using the latest version of Selenium IDE (3/5/2009) I've had success with using storeEval which evaluates Javascript expressions, giving you access to javascript string functions.

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 Selector:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文