背景: 用WebDriverWait時,一開始用的是presence_of_element_located,我對它的想法就是他就是用來等待元素出現。結果屢屢出問題。元素默認是隱藏的,導致等待過早的就結束了。
解決:去StackOverFlow查了一下,發現我應該用visibility_of_element_located。
原文:
Well, I would guess that presenceOfElementLocated will be slighty faster because it's just check elements presence on the page while the visibilityOfElementLocated has to check the presenceand whether is element visible. But I think it really doesn't matter from the performance perspective (what's the point if you save 0.001 second during this checking?), you better choose appropriate method depending on your use case. use presenceOfElementLocated when you don't care whether if element visible or not, you just need to know if it's on the page use visibilityOfElementLocated when you need to find element which should be also visible Look at the documentation for more info.
翻譯:
我猜 presence_of_element_located 肯定會稍微快一點,因為它僅僅檢查了頁面是否存在該元素,而visibility_of_element_located還必須檢查元素是否存在以及元素是否可見。
但是我認為從性能角度考慮,確實沒啥影響。最好根據使用情況來選擇正確的方法。
- presence_of_element_located: 當我們不關心元素是否可見,只關心元素是否存在在頁面中。
- visibility_of_element_located: 當我們需要找到元素,並且該元素也可見。