Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

java - In Selenium Webdriver which is better in terms of performance Linktext or css?

In Selenium it is always better to go locate the element with the Locator ID.

And the Least locator is XPath (Correct me if I'm wrong).

So is there any precedence or order in which a person should proceed with Locators to identify element?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is a little benchmark of different methods to locate an element on https://stackoverflow.com/questions :

Chrome 52, driver 2.22

15 ms for execute_script("return [].find.call(document.getElementsByTagName('a'), function(e){return e.textContent.trim() == 'Physics'});")
15 ms for find_element_by_css_selector("#footer-sites > table > tbody > tr:nth-child(2) > td:nth-child(7) > ol > li:nth-child(2) > a")
15 ms for find_element_by_css_selector("[title='go to page 2']")
15 ms for find_element_by_xpath("//*[@id='footer-sites']/table/tbody/tr[2]/td[7]/ol/li[3]/a")
16 ms for find_element_by_class_name("top-footer-links")
16 ms for find_element_by_css_selector("a[href$='math.stackexchange.com']")
16 ms for find_element_by_id("footer-sites")
16 ms for find_element_by_name("q")
16 ms for find_element_by_xpath("//a[text()='Ask Ubuntu']")
31 ms for find_element_by_xpath("//*[normalize-space(.)='Game Development']")
311 ms for find_element_by_link_text("Area 51")
343 ms for find_element_by_partial_link_text("Stack Apps")

Firefox 2.47, driver 2.53.0

16 ms for find_element_by_class_name("top-footer-links")
16 ms for find_element_by_css_selector("#footer-sites > table > tbody > tr:nth-child(2) > td:nth-child(7) > ol > li:nth-child(2) > a")
16 ms for find_element_by_css_selector("a[href$='math.stackexchange.com']")
16 ms for find_element_by_id("footer-sites")
16 ms for find_element_by_name("q")
23 ms for execute_script("return [].find.call(document.getElementsByTagName('a'), function(e){return e.textContent.trim() == 'Physics'});")
23 ms for find_element_by_css_selector("[title='go to page 2']")
47 ms for find_element_by_xpath("//*[@id='footer-sites']/table/tbody/tr[2]/td[7]/ol/li[3]/a")
47 ms for find_element_by_xpath("//a[text()='Ask Ubuntu']")
62 ms for find_element_by_xpath("//*[normalize-space(.)='Game Development']")
1625 ms for find_element_by_link_text("Area 51")
1726 ms for find_element_by_partial_link_text("Stack Apps")

Internet Explorer 11, driver 2.53.1

77 ms for find_element_by_name("q")
78 ms for execute_script("return [].find.call(document.getElementsByTagName('a'), function(e){return e.textContent.trim() == 'Physics'});")
78 ms for find_element_by_class_name("top-footer-links")
78 ms for find_element_by_id("footer-sites")
93 ms for find_element_by_xpath("//*[@id='footer-sites']/table/tbody/tr[2]/td[7]/ol/li[3]/a")
108 ms for find_element_by_xpath("//a[text()='Ask Ubuntu']")
109 ms for find_element_by_css_selector("#footer-sites > table > tbody > tr:nth-child(2) > td:nth-child(7) > ol > li:nth-child(2) > a")
125 ms for find_element_by_css_selector("[title='go to page 2']")
125 ms for find_element_by_css_selector("a[href$='math.stackexchange.com']")
140 ms for find_element_by_xpath("//*[normalize-space(.)='Game Development']")
801 ms for find_element_by_link_text("Area 51")
812 ms for find_element_by_partial_link_text("Stack Apps")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...