java 在java中检测seleniumRC的网页元素的最佳方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5313847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
best way to detect an element on a web page for seleniumRC in java
提问by powernit
What is the best way to detect an element on a webpage for automated testing using SeleniumRCusing Java?. I know that there are XPath elements and CSS elements, but which one is best?
使用 Java使用SeleniumRC检测网页上的元素以进行自动化测试的最佳方法是什么?我知道有 XPath 元素和 CSS 元素,但哪个最好?
thanks! Nitin
谢谢!尼丁
回答by eddy jones
X-Paths are accurate, indeed. However, they are also very fragile. Any change to the website that moves things around has the potential of breaking your tests. IDs are by far the least fragile, since it doesn't matter if your changes to a , your tests will always find it.
X-Paths 确实是准确的。然而,它们也非常脆弱。对网站进行任何移动的更改都有可能破坏您的测试。ID 是迄今为止最不脆弱的,因为无论您对 a 进行更改,您的测试总会找到它。
回答by Ardesco
In my opinion, the most accurate way is XPath as you can use XPath's to describe the exact position of an element within the DOM, however there are some instances where CSS locators work better than XPaths.
在我看来,最准确的方法是 XPath,因为您可以使用 XPath 来描述元素在 DOM 中的确切位置,但是在某些情况下,CSS 定位器比 XPath 工作得更好。
Using selenium's ID locator is the most simple, but unless the element you are looking for has an ID not always useful.
使用 selenium 的 ID 定位器是最简单的,但除非您要查找的元素具有 ID,否则并不总是有用。
The biggest negative when using XPath's is the performance with Selenium RC and browsers with bad JavaScript rendering engines (Think IE6/IE7). Quite a bit of work has been dome to try and rectify this so if you use well formed XPath's that only search specific parts of the DOM this problem should be minimal. The Selenium webdriver implementation is much faster.
使用 XPath 的最大缺点是 Selenium RC 的性能和带有糟糕 JavaScript 渲染引擎的浏览器(想想 IE6/IE7)。已经做了很多工作来尝试纠正这个问题,所以如果你使用格式良好的 XPath 只搜索 DOM 的特定部分,这个问题应该是最小的。Selenium webdriver 实现要快得多。
The summary is really there is no one answer it depends what you want out of a locator, and which browsers you are testing. I personally use XPath in 99% of instances.
总结是真的没有一个答案,这取决于您想从定位器中获得什么,以及您正在测试哪些浏览器。我个人在 99% 的情况下都使用 XPath。
I personally use XPath almost exclusively with the occasional CSS locator.
我个人几乎只将 XPath 与偶尔的 CSS 定位器一起使用。
回答by Josmas
CSS selectors are faster but not always available; You can always rely on XPath but could take a toll on performance.
CSS 选择器更快但并不总是可用;您始终可以依赖 XPath,但可能会影响性能。
回答by Luixv
Xpath is the way I've used. It will help if your nodes have unique id's.
Xpath 是我使用的方式。如果您的节点具有唯一 ID,这将有所帮助。
回答by Sam Backus
The best and fastest way to find an element is by id. In many browsers, the time to find an element by it's id is linear or even constant. very fast.
查找元素的最佳和最快方法是通过 id。在许多浏览器中,通过 id 查找元素的时间是线性的,甚至是恒定的。非常快。
For example, given an element defined as:
例如,给定一个元素定义为:
<input type="text" name="passwd" id="passwd-id" />
You should find it like this:
你应该会发现它是这样的:
selenium.type("passwd-id", "test");
Or if you're using the webdriver API:
或者,如果您使用的是 webdriver API:
element = driver.findElement(By.id("passwd-id"));
If the elements on your pages don't have ids, add them! or ask the developers to add them!
如果页面上的元素没有 ID,请添加它们!或要求开发人员添加它们!
The next best way to find elements is by name. That's pretty quick as well, especially if the name is unique on the page.
查找元素的下一个最佳方法是按名称。这也非常快,特别是如果页面上的名称是唯一的。
For example:
例如:
selenium.type("passwd", "test");
Or if you're using the webdriver API:
或者,如果您使用的是 webdriver API:
element = driver.findElement(By.name("passwd"));
The third way of finding an element is using CSS selectors. Modern browsers are very good at locating elements this way.
查找元素的第三种方法是使用 CSS 选择器。现代浏览器非常擅长以这种方式定位元素。
The worst way to locate an element is using xpath. xpath is slow and brittle and hard to read.
定位元素的最糟糕的方法是使用 xpath。xpath 缓慢而脆弱且难以阅读。
Simon Stewart, a major contributor to Selenium, answers this question here: http://www.youtube.com/watch?v=oX-0Mt5zju0&feature=relatedlook for timestamp 39:00
Selenium 的主要贡献者 Simon Stewart 在这里回答了这个问题:http: //www.youtube.com/watch?v=oX-0Mt5zju0&feature=related look for timestamp 39:00
There's also good info here: http://seleniumhq.org/docs/02_selenium_ide.html#locating-elements
这里也有很好的信息:http: //seleniumhq.org/docs/02_selenium_ide.html#locating-elements