作者FacetheFaith (己立立人己达达人)
看板Soft_Job
标题[请益] 怎样的XPath语法可以较快找到element
时间Tue May 8 11:04:44 2018
请教版上大大
有一树状结构如下:
<div id='wd-Inbox-mainpage'>
<div>
<div>
<ul data-automation-id='inbox_item_list'>
要用XPath来定位到该ul元素,目前有以下几种写法都可行
1. .//ul[@data-automation-id='inbox_item_list']
. 2. .//*[@data-automation-id='inbox_item_list']
3. .//*[@id='wd-Inbox-mainpage']/div[1]/div[1]/ul
4. .//*[@id='wd-Inbox-mainpage']/div[1]/div[1]/ul[@data-automation-id=
'inbox_item_list']
请问哪种方法能最快找到该元素?
另外使用.//或//对搜寻速度会有影响吗,哪个较快找到?
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 61.219.237.246
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Soft_Job/M.1525748687.A.1DF.html
1F:→ vi000246: 在chrome的F12 直接按右键复制xpath的最快 05/08 11:40
2F:→ vi000246: 你可以写个计时器 算算捞一万次两种写法的差别 05/08 11:41
3F:推 ian90911: 原来chrome f12可以直接抓xpath 学到一招了 05/08 12:04
4F:→ alog: 其实 element 没有量其实还好 最终还是要看 library 的实作/ 05/08 12:24
5F:→ alog: 搜寻方式跟特性 05/08 12:24
6F:→ alog: 要找就找 ul 跟 data-automation-id 那个就可以了 05/08 12:27
7F:推 alog: 除非你找的 element 有特别的规则 例如抓要在特定的阶层底下 05/08 12:29
8F:→ alog: 这个东西 其他的忽略 05/08 12:29
9F:→ alog: 否则应该要将xpath简化 但回到一开始讲的 05/08 12:29
10F:→ alog: 元素没破千破万应该是还好 那个算多少次都一样 05/08 12:30
11F:→ FacetheFaith: Chrome抓到的XPath是//*[@id='wd-Inbox-mainpage']/ 05/08 12:34
12F:→ FacetheFaith: div[1]/div[1]/ul,所以用这个比较好吗 05/08 12:34
13F:推 TitanEric: 个人觉得xpath不好懂 我都用selector 05/08 12:38
14F:→ alog: Google 那个是方便抓 不会有特别优化 05/08 13:00
15F:推 alog: 只有element 带有id 才会特别优化 因为 id 搜寻底层会有比 05/08 13:02
16F:→ alog: 较快的优化处理 05/08 13:02
17F:→ FacetheFaith: 那id跟data-automation-id哪个比较快?後者是怎麽产 05/08 13:18
18F:→ FacetheFaith: 生的? 05/08 13:18
19F:→ alog: id 但是你要找的资料是 带有data-automation-id 的ul 05/08 13:18
20F:→ alog: 所以东西简单处理就好 05/08 13:19
21F:→ FacetheFaith: 所以会建议选1吗 05/08 13:21
22F:推 alog: 嗯 因为你找的类型似乎没有这麽强调阶层之间的关系 05/08 13:22
23F:嘘 thekensg: 是关工作版屁事啊 05/08 19:31
24F:推 ripple0129: 页面是有这麽多DOM需要考量到效能啊@@其实这个想一下 05/10 00:52
25F:→ ripple0129: 也知道,第一个比对,id通常只有一个所以可以快速且 05/10 00:52
26F:→ ripple0129: 正确定位效能一定是最快的 05/10 00:52