Locator performance & considerations

本文探讨了在Selenium测试中使用CSS选择器与XPath进行网页元素定位的区别。重点介绍了CSS选择器相较于XPath的优势,包括速度更快、更易读、与jQuery定位策略一致等,并提到了某些情况下使用CSS选择器可能存在的限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

When Doing Selenium tests, to locate the certain web elements, there are several methods.

Like By.id, By.className, By.tagName, By.name... But among them all, we use By.xPath and By.cssSelector to specify the elements with complex structure in the web page.


Now, while we are writing script to find and locate those web elements, we should be aware of the differences of these two "locating stratogy".


There are  four reasons for using CSS Locators: 

1. They’re faster
2. They’re more readable
3. CSS is jQuery’s locating strategy
4. No one else uses XPATH anyways!

 

 the speed of XPath vs the speed of CSS Locators can be viewed in performance metric script .

There wasn’t much of a difference in Firefox, Safari, or Chrome, but with IE, the results were undeniable. Take a look:


 there is a tool called  cssify It does the handy work of translating your XPaths to CSS automatically.


Is there any case  when you shouldn’t use CSS Locators (yes, there are a few cases where it is not the right tool for the job). 

 

You should see this 

CSS SELECTORS AND PSEUDO SELECTORS BROWSER COMPATIBILITY 


The above picture shows many selector Usage are not supported before IE9 version and firefox 4 version. When writing cssSelectors we should take these into account.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值