为Selenium Webdriver中的元素定位器的层次结构提供建议 [英] Advise on hierarchy for element locators in selenium webdriver

查看:126
本文介绍了为Selenium Webdriver中的元素定位器的层次结构提供建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我知道如何在Selenium Webdriver中搜索到usr的页面上的不同元素以进行测试,例如通过xpath,id,css等进行搜索,但在有人建议使用哪个层次结构以及为什么之后?例如.恢复搜索ID之前,我们应该使用xpath定位符吗?

whilst I am aware of how to search for the different elements on a page in Selenium Webdriver to usr for testing, e.g.by xpath, id, css etc, I was after some advise as to which hierarchy to use and why? E.g. should we use xpath locators before reverting to searching for the id?

了解这一点将使我能够以正确的方式构建测试,从而使其尽快运行.

Having an awareness of this will allow me to build my tests in the correct way so that they run as quickly as possible.

推荐答案

根据我对硒的经验,我认为以下是最好的层次结构:

According to my experience with selenium I think following is best hierarchy :

  1. Id

  1. Id

名称

链接文本

部分链接文本

标签名称

类名

Css

xpath

每个元素的

1-通过ID 始终是唯一的,因此对我们来说很容易访问,对于Web驱动程序来说是快速的.大多数情况下,我们应该将ID作为查找元素的优先级.

1 - By ID for each and every element is always unique so easy to access for us and fast for web driver.Mostly we should prefer Id as first priority to find element.

2-如果通过ID 不好,那么我们应该查看名称".但是很多时候会出现多个名称,因此如果同一名称属性存在多个名称,则需要注意这一点.

2 - If By ID is not Ok then we should look at "Name". But many time it happens that multiple name is there so need to take care for this if same name attribute is multiple times there.

3-通过链接文本"&如果站点中的所有链接都有唯一的链接文本,按部分链接文本" 也是查找元素的好方法.

3 - "By linktext" & "By partial linktext" are also great ways to find element if all links in site have unique link text.

4-如果您的网站或页面具有诸如dropdown,复选框之类的组元素,则可以使用属性"按标记名" 查找元素.

4 - If your site or page has group elements like dropdown , checkboxes then you can use attribute "By tagname" to find element.

5-通过类名" 查找元素是一种罕见的方法,因为大多数网站对多个项目使用相同的类名来应用CSS.但是请确保您可以将其用于唯一的类名.

5 - By "Class name" is rare way to find element because most of website using same class name for multiple items to apply css. But sure you can use it for unique class names.

6-如果需要快速找到复杂的元素,请使用属性"Css选择器" 查找元素.

6 - If you need speed to locate complex elements then kindly use attribute "Css selector" to find element.

7-主要使用"Xpath" 属性导航xml文档.仍然可以将其用于网页或网站.

7 - Main use of "Xpath" attribute to navigate for xml documents. Still you can use it for web page or site.

注意:基本上,元素定位器的选择取决于您的页面,页面包含哪种类型的事物,例如太多的下拉列表,复选框,文本框等等.但是我给出的步骤是一般性的,您可以在任何站点中遵循这些步骤来查找元素并以流畅的方式执行程序.

Note : Basically selection of element locator depends on your page that which type of things page contains like too many dropdowns , checkboxes, textboxes and a lot more. But my given steps are general which you can follow for any site to find elements and execute program in smooth way.

希望以上对您有很大帮助.

Hope above will help you a lot.

其他参考 查看全文

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