作者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/m.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