Skip to content

Instantly share code, notes, and snippets.

@ryanking13
Created December 10, 2021 06:43
Show Gist options
  • Select an option

  • Save ryanking13/a7f0676b88bb61a46c576733f2444bcb to your computer and use it in GitHub Desktop.

Select an option

Save ryanking13/a7f0676b88bb61a46c576733f2444bcb to your computer and use it in GitHub Desktop.
Performance comparison between Selenium and Playwright
#!/usr/bin/env python
# pip install selenium playwright
# playwright install
import timeit
import time
import selenium
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options as ChromeOptions
from selenium.webdriver import Firefox
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from playwright.sync_api import sync_playwright
def selenium_chrome(repeat):
options = ChromeOptions()
options.add_argument("--headless")
options.add_argument("--no-sandbox")
options.add_argument("--js-flags=--expose-gc")
for i in range(repeat):
driver = Chrome(options=options)
driver.quit()
def selenium_firefox(repeat):
options = FirefoxOptions()
options.add_argument("--headless")
for i in range(repeat):
driver = Firefox(executable_path="geckodriver", options=options)
driver.quit()
def playwright_chrome(repeat):
with sync_playwright() as p:
for i in range(repeat):
browser = p.chromium.launch()
context = browser.new_context()
page = context.new_page()
browser.close()
def playwright_firefox(repeat):
with sync_playwright() as p:
for i in range(repeat):
browser = p.firefox.launch()
context = browser.new_context()
page = context.new_page()
browser.close()
def playwright_chrome_contexts(repeat):
with sync_playwright() as p:
browser = p.chromium.launch()
for i in range(repeat):
context = browser.new_context()
page = context.new_page()
context.close()
page.close()
browser.close()
def playwright_firefox_contexts(repeat):
with sync_playwright() as p:
browser = p.firefox.launch()
for i in range(repeat):
context = browser.new_context()
page = context.new_page()
context.close()
page.close()
browser.close()
repeat = 10
print(
"Playwright Chrome:",
timeit.timeit(
f"playwright_chrome({repeat})",
number=1,
setup="from __main__ import playwright_chrome",
),
)
print(
"Playwright Firefox:",
timeit.timeit(
f"playwright_firefox({repeat})",
number=1,
setup="from __main__ import playwright_firefox",
),
)
print(
"Playwright Chrome (context):",
timeit.timeit(
f"playwright_chrome_contexts({repeat})",
number=1,
setup="from __main__ import playwright_chrome_contexts",
),
)
print(
"Playwright Firefox (context):",
timeit.timeit(
f"playwright_firefox_contexts({repeat})",
number=1,
setup="from __main__ import playwright_firefox_contexts",
),
)
print(
"Selenium Chrome:",
timeit.timeit(
f"selenium_chrome({repeat})",
number=1,
setup="from __main__ import selenium_chrome",
),
)
print(
"Selenium Firefox:",
timeit.timeit(
f"selenium_firefox({repeat})",
number=1,
setup="from __main__ import selenium_firefox",
),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment