testing - <="x"的标签 > 的 Selenium 定位器

标签 testing selenium watin selenium-rc web-testing

在 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 中做到这一点?

最佳答案

这个有效:

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

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

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

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

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

关于testing - <="x"的标签 > 的 Selenium 定位器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2030711/

相关文章:

java - 测试组的 TestNG 执行顺序

ruby-on-rails - 在 rails4 中放置测试 gem 的位置

testing - 我应该如何在 ember 中进行测试?

python Selenium : getting rid of Firefox first run screen

javascript - 跨多个浏览器测试 Javascript

ubuntu - Selenium 无法启动 - Ubuntu 14

c# - Selenium 测试查找显示是否为 None

c# - Web自动化Watin问题

c# - C# 的设计模式和基于字符串参数调用不同的方法

automated-tests - Watin 和 Fluent 自动化 - 引用当前浏览器