在 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/