在Selenium Webdriver中,在性能Linktext或css方面哪个更好? [英] In Selenium Webdriver which is better in terms of performance Linktext or css?

查看:138
本文介绍了在Selenium Webdriver中,在性能Linktext或css方面哪个更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Selenium 中,最好找到定位器 ID 的元素。

In Selenium it is always better to go locate the element with the Locator ID.

最小定位器是 XPath (如果我错了,请纠正我)。

And the Least locator is XPath (Correct me if I'm wrong).

因此,任何人都应该使用 定位器 来确定元素

So is there any precedence or order in which a person should proceed with Locators to identify element?

推荐答案

我只是写一些来自这个参考文献已由GK27在评论中提供,这里写的目的是要清楚,如果页面没有找到时间,用户可以从这里查看。

I'm just writing some points from this reference which is already provided by GK27 in comments, The purpose of writing here is to be clear if page has not found some time, user can view from here.

因此,找到元素的更好方法是,优先级应该在列表中 [id,name,linkText,partialLinkText,tagName ,className,cssSelector,xpath] 此处列表中的第一个值 id 包含第一优先级,依此类推。

So the better way of locating the element, Priority wise should be in the list [id, name, linkText, partialLinkText, tagName, className, cssSelector, xpath] here first value id in the list contains first priority and so on.

按ID查找元素:

找到最有效的方法和首选方式网页上的元素是按 ID ID 将是网页上唯一可以轻松识别的内容。
ID是最安全,最快的定位器选项,应始终是第一选择

The most efficient way and preferred way to locate an element on a web page is By ID. ID will be the unique on web page which can be easily identified. IDs are the safest and fastest locator option and should always be the first choice

按名称定位元素:

当没有Id可供使用时,下一个值得一看的是所需元素是否具有name属性。但请确保名称不能始终唯一。如果有多个名称,Selenium将始终对第一个匹配元素执行操作

通过LinkText定位元素:

查找包含链接文本的元素非常简单。但请确保网页上只有一个唯一的链接。如果有多个链接具有相同的链接文本(例如重复的页眉和页脚菜单链接),在这种情况下,Selenium将对带有链接的第一个匹配元素执行操作。

通过部分LinkText定位元素

LinkText PartialLinkText 也可以使用相同的模式,唯一的区别是它与部分文本的匹配链接意味着使用包含。

In the same way as LinkText, PartialLinkText also works in the same pattern, only difference is that it's match link with partial text means uses contains.

通过TagName定位元素:

TagName 可以与组元素一起使用,例如,选择和复选框/下拉列表。

TagName can be used with Group elements like , Select and check-boxes / dropdowns.

按类名定位元素:

如果我们只使用 findElementByClassName ,可能有多个具有相同类名的元素,请确保它是只有一个。如果不是,您需要使用 classname 及其子元素进行扩展。

There may be multiple elements with the same class name, if we just use findElementByClassName, make sure it is only one. If not the you need to extend using the classname and its sub elements.

CSS选择器:

CSS主要用于为网页提供样式规则,我们可以用它来识别网页中的一个或多个元素用css。
如果你开始使用css选择器来识别元素,那么与 XPath 相比,你会喜欢它的速度。
我们可以使用Css选择器来确保脚本在IE浏览器中以相同的速度运行。 CSS选择器始终是在页面中定位复杂元素的最佳方式。

如果您需要使用复杂选择器查找元素,我通常建议使用CSS选择器,如果可能的话。它不像XPath那么灵活,但会覆盖许多相同的情况,而不会在XP上表现出对XP的极端性能损失。

CSS mainly used to provide style rules for the web pages and we can use for identifying one or more elements in the web page using css. If you start using css selectors to identify elements, you will love the speed when compared with XPath. We can you use Css Selectors to make sure scripts run with the same speed in IE browser. CSS selector is always the best possible way to locate complex elements in the page. If you have a need to find an element using a complex selector, I usually recommend using CSS Selectors, if possible. It's not quite as flexible as XPath, but will cover many of the same cases, without exhibiting the extreme performance penalty on IE that XPath can.

XPath选择器:

XPath 查找元素对于使用非常有用的元素查找元素非常有用复杂的选择器,并且是最灵活的选择策略,但它有可能非常慢,特别是在 IE 中。在 IE 6,7或8中,按 XPath 查找可能比执行相同的操作慢一个数量级火狐 IE 不提供原生 XPath-over-HTML 解决方案,因此项目必须使用JavaScript XPath 实现,以及 IE 的旧版本中的JavaScript引擎确实要慢得多。

Finding elements by XPath is useful for finding elements using very complex selectors, and is the most flexible selection strategy, but it has the potential to be very slow, particularly in IE. In IE 6, 7, or 8, finding by XPath can be an order of magnitude slower than doing the same in Firefox. IE provides no native XPath-over-HTML solution, so the project must use a JavaScript XPath implementation, and the JavaScript engine in legacy versions of IE really is that much slower.

有两种类型的xpath

There are two types of xpath


  1. 原生Xpath,就像指导xpath直接进行一样。比如
    示例:
    html / head / body / table / tr / td

  1. Native Xpath, it is like directing the xpath to go in direct way. like Example: html/head/body/table/tr/td

这里指定原生路径的优点是,找到一个元素非常容易,因为我们提到直接路径。但是如果路径有任何变化(如果添加/删除了某些东西)那么xpath就会中断。

Here the advantage of specifying native path is, finding an element is very easy as we are mention the direct path. But if there is any change in the path (if some thing has been added/removed) then that xpath will break.


  1. 相对Xpath。
    在相对xpath中我们将提供相对路径,就像我们将告诉xpath通过告诉它们之间的路径来找到一个元素。
    这里的优点是,如果html中的任何变化都可以正常工作,除非特定路径发生变化。查找地址将非常困难,因为它需要检查每个节点以找到该路径。

这篇关于在Selenium Webdriver中,在性能Linktext或css方面哪个更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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