在XPath之外的Selenium Webdriver中找到元素的最佳方法是什么? [英] Which is the best way to locate an element in selenium webdriver other than XPath?

查看:121
本文介绍了在XPath之外的Selenium Webdriver中找到元素的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试的应用程序正在快速开发,并且不断增加新功能,需要对测试XPath进行更改.因此,由于XPath的更改,以前成功的Selenium脚本现在失败了.是否有任何可靠的方式来定位元素(永远不变)?仅供参考,我考虑过使用ID,但是我的应用程序没有每个元素的ID,因为不建议在代码中提供ID.

The application which I'm testing is fast developing, and new features keep being adding, requiring changes to the testing XPaths. So the selenium scripts which were successful before now failed as the XPaths have changed. Is there any reliable way to locate element (which will never change)? FYI, I thought of using ID's but my application does not have ID's for each and every element as it is not recommended to give ID's in the code.

推荐答案

如果无法在不断变化的界面上施加@id约束,则一种替代方法是使用

If you cannot impose @id discipline on the interface that keeps changing, one alternative is to use CSS selectors.

编写更强大的XPath的另一种方法:

Another alternative to write more robust XPath:

  1. 请谨慎使用descendent-or-self轴(//):

使用//uniquepart//uniquepart/further/path而不是/some/long/and/brittle/path/uniquepart绕过可能更改的内容.

Rather than /some/long/and/brittle/path/uniquepart use //uniquepart or //uniquepart/further/path to bypass that which is likely to change.

不要过度指定标签匹配.

Don't overspecify label matching.

使用不区分大小写的contains(),并尝试匹配可能在整个标签中保持不变的标签的关键部分界面更改.

Use case-insensitive contains(), and try to match critical parts of labels that are likely to remain invariant across interface changes.

这篇关于在XPath之外的Selenium Webdriver中找到元素的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆