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

前端 未结 2 1400
醉酒成梦
醉酒成梦 2020-12-11 17:34

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

And the Least locator is XPath (Correct me if

2条回答
  •  悲哀的现实
    2020-12-11 18:07

    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")
    

提交回复
热议问题