🔗

[Python] Selenium CSSセレクターとXPathの速度比較

2022/06/21に公開

はじめに

Seleniumの処理を速く出来ないかと調べてみたら要素を取得するのにCSSセレクターの方がXPathより速いと記述がありました。
そのため、軽く調べてみました。

結論

速度は変わらないようです。

環境

Python 3.10.4 (tags/v3.10.4:9d38120, Mar 23 2022, 23:13:41) [MSC v.1929 64 bit (AMD64)]
selenium 4.1.3
geckodriver 0.31.0 (b617178ef491 2022-04-06 11:57 +0000)

コード

# standard
import os
import timeit
import gc

# selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from selenium.webdriver.firefox.service import Service as FirefoxService

import const


def getExecutablePath(drive):
  if drive == "d":
    return const.ExecutablePathD
  return const.ExecutablePathC


def getDriver():
  options = FirefoxOptions()
  options.add_argument('-safe-mode')
  options.set_preference("browser.cache.disk.enable", False)
  service = FirefoxService(executable_path=getExecutablePath("d"), log_path=os.path.devnull)
  driver = webdriver.Firefox(service=service, options=options)
  driver.set_page_load_timeout(300)
  return driver


driver = getDriver()
driver.get(r"https://ja.wikipedia.org/wiki/%E3%83%8D%E3%82%B3")


def findElementCSS(driver, cssSelector):
  element = driver.find_element(By.CSS_SELECTOR, cssSelector)
  # print(element.get_attribute("textContent"))


def findElementXpath(driver, xpath):
  element = driver.find_element(By.XPATH, xpath)
  # print(element.get_attribute("textContent"))


css = "div.thumb:nth-child(59) > div:nth-child(1) > div:nth-child(2)"
# findElementCSS(driver, css)
xpath = "/html/body/div[3]/div[3]/div[5]/div[1]/div[13]/div/div"
# findElementXpath(driver, xpath)

number = 5000
repeat = 5
result = timeit.repeat(lambda: findElementXpath(driver, xpath), setup=gc.collect, number=number, repeat=repeat)
print("Xpath,", result)
result = timeit.repeat(lambda: findElementCSS(driver, css), setup=gc.collect, number=number, repeat=repeat)
print("CSS", result)

result = timeit.repeat(lambda: findElementCSS(driver, css), setup=gc.collect, number=number, repeat=repeat)
print("CSS", result)
result = timeit.repeat(lambda: findElementXpath(driver, xpath), setup=gc.collect, number=number, repeat=repeat)
print("Xpath,", result)

driver.quit()
実行結果
Xpath, [6.978661499997543, 6.653559300000779, 6.598208700001123, 6.597355799996876, 6.897136400002637]
CSS [6.886301700003969, 6.926162299998396, 6.8211366000032285, 7.246282899999642, 7.191129499995441]
CSS [6.970818499998131, 7.338858499999333, 6.79556749999756, 6.692802400000801, 6.732309799997893]
Xpath, [6.734272900001088, 6.7834651000011945, 6.677139999999781, 6.676666600003955, 7.114523399999598]

Discussion