发布于 2024-08-17 05:48:17 字数 592 浏览 3 评论 0原文

对于 ASP.NET,标签 ID 非常不稳定,因此为了使我的测试更加稳健,我想通过标签文本来定位元素。我用 WatiN 玩过一些,它完美地做到了这一点,但这个项目现在似乎已经死了,所以我想在决定框架之前我也应该研究一下 Selenium。

我的 html 看起来像这样,

<label for="ctl00_content_loginForm_ctl01_username">Username</label>:
<input type="text" id="ctl00_content_loginForm_ctl01_username" />

但我不想输入:

selenium.Type("ctl00_content_loginForm_ctl01_username", "xxx");

这太依赖 ID 了。在 WatiN 中,我会写:

browser.TextField(Find.ByLabelText("Username")).TypeText("xxx");

有没有办法在 Selenium 中做到这一点?

With ASP.NET the tag IDs are pretty volatile so to make my tests more robust I want to locate elements by their label texts. I have played some with WatiN and it does this perfectly but that project seem kind of dead nowadays so I thought I'd look into Selenium as well before I decide on a framework.

I have html that looks something like this

<label for="ctl00_content_loginForm_ctl01_username">Username</label>:
<input type="text" id="ctl00_content_loginForm_ctl01_username" />

I don't want to type:

selenium.Type("ctl00_content_loginForm_ctl01_username", "xxx");

That is too reliant on the ID. In WatiN I'd write:

browser.TextField(Find.ByLabelText("Username")).TypeText("xxx");

Is there a way to do this in Selenium?

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

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

发布评论

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

评论(4

青柠芒果 2024-08-24 05:48:17

这有效:

//input[@id=(//label[text()="Username"]/@for)]

说明:由于您正在查找输入:

//input[@id=("ctl00_content_loginForm_ctl01_username")]

将“ctl00_content_loginForm_ctl01_username”替换为标签的属性值:

//label[text()="Username"]/@for

This works:

//input[@id=(//label[text()="Username"]/@for)]

Explanation: Since you are looking for the input:

//input[@id=("ctl00_content_loginForm_ctl01_username")]

replace the "ctl00_content_loginForm_ctl01_username" by the attribute's value of the label:

//label[text()="Username"]/@for
隱形的亼 2024-08-24 05:48:17

我相信您可以通过以下方式执行此操作:

selenium.Type(selenium.getAttribute("//label[text()='Username']/@for"), "xxx");

text()='Username' 位通过其innerHTML 获取您想要的标签,然后/@for 返回其“for”属性的值。

注意:这尚未经过测试(对此表示歉意!),但我认为它会起作用,基于 IDE 插件中的一些工具

I believe you can do this with the following:

selenium.Type(selenium.getAttribute("//label[text()='Username']/@for"), "xxx");

The text()='Username' bit gets the label you want by its innerHTML, then the /@for gives you back the value of its "for" attribute.

Heads up: this is not tested (apologies for that!) but I think it'll work, based on some tooling around in the IDE plugin

赴月观长安 2024-08-24 05:48:17

好吧,这可能已经有一岁了,但是他们嘿。这将选择包含文本“用户名”的标签下的第一个输入。

//label[text()='Username']/input

我通常更喜欢使用 contains() ,因为我发现某些浏览器会在偶尔的元素中添加烦人的空格:

//label[contains(., 'Username')]/input

请注意,输入之前的单斜杠表示它只会向下查找一级,其中双斜杠将检查标签下的所有级别。使用 XPather for Firefox 来创建和检查您的 XPath,它非常有用。

Ok, this may be a year old but what they hey. This will select the first input under a label containing the text 'Username'.

//label[text()='Username']/input

I generally prefer using contains() as I find that some browsers are adding annoying spaces into the occasional element:

//label[contains(., 'Username')]/input

Note that the single slash before input denotes it will only look one level down, where the double slash would check all levels under the label. Use XPather for Firefox to create and check your XPaths, it's very useful.

两相知 2024-08-24 05:48:17

是的,您可以使用 XPath、CSS 或 DOM 定位器来识别您的元素。在此示例中,您的 XPath 可能类似于 //lable[@for='ctl00_content_loginForm_ctl01_username'] 来标识该特定标签。

Yes, you can use XPath, CSS or DOM locators to identify your element. In this example your XPath could look like //lable[@for='ctl00_content_loginForm_ctl01_username'] to identify that particular label.

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