使用Selenium Webdriver查找元素的最快和最慢的方法是什么? [英] What is the fastest and slowest ways of finding elements using Selenium Webdriver?

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

问题描述

我已经阅读了很多论坛和博客,但是我仍然不相信这一点.最近,我的一个朋友在一次采访中被问到了这个问题.

I've read plenty of forums and blogs but somehow I ain't convinced on this yet. Recently a friend of mine was asked this question in an interview.

推荐答案

  1. ID:id将显然是选择元素的最优选属性(除非生成该元素).由于id是唯一的,这也是定位元素的最快方法.

  1. ID: id will obviously be the most preferred attribute to select an element (unless it is generated). It is also the fastest way to locate an element since the id is unique.

名称/标签名称:此定位器很好地用于查找具有相同名称的多个元素.

Name / tagName: This locator works well for finding multiple elements with the same name.

Linktext/PartialLinkText-定位器非常适合导航活动.

Linktext / PartialLinkText - Locator is good for navigation activites.

CSS vs Xpath-

CSS vs Xpath -

您可以通过有关CSS与XPath辩论的问题.根据答案,CSS可读性更高,速度更快(尤其是在IE较低版本中),并且可以在所有浏览器中使用.

You may go through this question for the CSS vs XPath debate. As per the answers, CSS is more readable, faster (especially in IE lower versions), and will work in all browsers.

Xpath的一大优点是它支持'contains',而在CSS中已弃用(至少在Selenium中). 但是,包含"在Jsoup库中的工作方式就像是一种魅力,相对于Xsoup(Xpath选择器),我更喜欢使用Jsoup(CSS选择器)选择文档中的元素.

A big plus with Xpath is that it supports 'contains' whereas it is deprecated in CSS (atleast in Selenium). But, 'contains' works like a charm with the Jsoup library and I preferred using Jsoup(CSS selector) over Xsoup(Xpath selector) for selecting elements in the document.

就Selenium而言,许多人更喜欢使用XPath(至少这是我所观察到的),但是它必须格式正确,以便在网页更改时不会发生太大变化.

As far as Selenium is concerned, many prefer to use XPath (atleast this is what I've observed), but it has to be well formed so that it doesn't change a lot when web page changes.

另一篇很好的文章是- http://elementalselenium.com/tips/32-xpath-vs-css

Another good article would be this - http://elementalselenium.com/tips/32-xpath-vs-css

这篇关于使用Selenium Webdriver查找元素的最快和最慢的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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