使用Selenium Webdriver查找元素的最快和最慢的方法是什么?

编程入门 行业动态 更新时间:2024-10-10 15:25:57
本文介绍了使用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.

推荐答案

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

  • 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.

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

    Another good article would be this - elementalselenium/tips/32-xpath-vs-css

  • 更多推荐

    使用Selenium Webdriver查找元素的最快和最慢的方法是什么?

    本文发布于:2023-11-27 22:44:10,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1639907.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:最慢   元素   最快   方法   Selenium

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!