<label for=“x">的硒定位器 [英] Selenium locator for <label for="x">
问题描述
在 ASP.NET 中,标签 ID 非常不稳定,因此为了使我的测试更加健壮,我想通过标签文本来定位元素.我玩过一些 WatiN,它完美地做到了这一点,但现在这个项目似乎已经死了,所以我想在我决定一个框架之前我也会研究 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.
我的 html 看起来像这样
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" />
我不想输入:
selenium.Type("ctl00_content_loginForm_ctl01_username", "xxx");
那太依赖ID了.在 WatiN 我会写:
That is too reliant on the ID. In WatiN I'd write:
browser.TextField(Find.ByLabelText("Username")).TypeText("xxx");
有没有办法在 Selenium 中做到这一点?
Is there a way to do this in Selenium?
推荐答案
我相信您可以通过以下方式做到这一点:
I believe you can do this with the following:
selenium.Type(selenium.getAttribute("//label[text()='Username']/@for"), "xxx");
text()='Username' 位通过其innerHTML 获取您想要的标签,然后/@for 将返回其for"属性的值.
The text()='Username' bit gets the label you want by its innerHTML, then the /@for gives you back the value of its "for" attribute.
注意:这没有经过测试(为此道歉!)但我认为它会起作用,基于 IDE 插件中的一些工具
Heads up: this is not tested (apologies for that!) but I think it'll work, based on some tooling around in the IDE plugin
这篇关于<label for=“x">的硒定位器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!