CAPSOLVER
Blog
Using Playwright with Ruby: Step-by-Step Guide for 2024

Using Playwright with Ruby: Step-by-Step Guide for 2024

Logo of CapSolver

Lucas Mitchell

Automation Engineer

02-Sep-2024

Using Playwright with Ruby: Step-by-Step Guide for 2024

Web scraping has become an essential skill for developers who need to gather data from websites. Playwright, a powerful browser automation tool, is often used for this purpose. In this guide, we will explore how to use Playwright with Ruby to scrape data from a website. We will walk through a practical example using the website Quotes to Scrape.

Prerequisites

Before we begin, make sure you have the following installed on your machine:

You can install the necessary dependencies by running:

bash Copy
gem install playwright-ruby-client

Setting Up Playwright

After installing the playwright-ruby-client gem, you need to set up Playwright in your Ruby script. Hereโ€™s how you can do it:

ruby Copy
require 'playwright'

Playwright.create(playwright_cli_executable_path: '/path/to/node_modules/.bin/playwright') do |playwright|
  browser = playwright.chromium.launch(headless: false)
  page = browser.new_page
  page.goto('http://quotes.toscrape.com/')
  
  # Example scraping code will go here
  
  browser.close
end

Replace '/path/to/node_modules/.bin/playwright' with the actual path to the Playwright CLI on your system.

Scraping Quotes from the Website

Now, let's write the code to scrape quotes from the website. We will extract the text of each quote and the corresponding author.

ruby Copy
require 'playwright'

Playwright.create(playwright_cli_executable_path: '/path/to/node_modules/.bin/playwright') do |playwright|
  browser = playwright.chromium.launch(headless: false)
  page = browser.new_page
  page.goto('http://quotes.toscrape.com/')
  
  quotes = page.query_selector_all('.quote')

  quotes.each do |quote|
    quote_text = quote.query_selector('.text').text_content.strip
    author = quote.query_selector('.author').text_content.strip
    puts "#{quote_text} - #{author}"
  end

  browser.close
end

Explanation

  1. Navigating to the Website: The script navigates to the http://quotes.toscrape.com/ URL using the page.goto method.
  2. Selecting Quotes: We use page.query_selector_all('.quote') to select all elements that have the class quote.
  3. Extracting Text and Author: For each quote, we extract the text content and the author using the respective selectors.
  4. Output: Finally, we print each quote followed by its author to the console.

Running the Script

You can run this Ruby script from your terminal:

bash Copy
ruby playwright_scraper.rb

Make sure to replace playwright_scraper.rb with the filename of your script.

Handling CAPTCHA Challenges with Playwright and Ruby

CAPTCHA challenges are a common obstacle when scraping websites, designed to differentiate between human users and bots. For developers using Playwright with Ruby, overcoming these challenges is essential to successfully automate data extraction. In this guide, we'll explore how to integrate CAPTCHA solving services using CapSolver with Playwright. Depending on the type of CAPTCHA implemented by the website, you can either configure CapSolver via an extension (the simplest method) or through their API for more advanced use cases.

For detailed instructions on setting up the extension, visit CapSolver Extension Documentation. For API integration, refer to the CapSolver API Documentation.

Conclusion

This guide has shown you how to set up Playwright with Ruby and scrape data from a website. The example used here is simple but can be expanded for more complex tasks. Playwrightโ€™s ability to automate browser tasks makes it a powerful tool for web scraping and testing.

Happy scraping!

Compliance Disclaimer: The information provided on this blog is for informational purposes only. CapSolver is committed to compliance with all applicable laws and regulations. The use of the CapSolver network for illegal, fraudulent, or abusive activities is strictly prohibited and will be investigated. Our captcha-solving solutions enhance user experience while ensuring 100% compliance in helping solve captcha difficulties during public data crawling. We encourage responsible use of our services. For more information, please visit our Terms of Service and Privacy Policy.

More

Browser Use vs Browserbase: Which Browser Automation Tool Is Better for AI Agents?
Browser Use vs Browserbase: Which Browser Automation Tool Is Better for AI Agents?

Compare Browser Use vs Browserbase for AI agent automation. Discover features, pricing, and how to solve CAPTCHAs with CapSolver for seamless workflows.

AI
Logo of CapSolver

Anh Tuan

27-Jan-2026

IP Bans in 2026: How They Work and Practical Ways to Bypass Them
IP Bans in 2026: How They Work and Practical Ways to Bypass Them

Learn how to bypass ip ban in 2026 with our comprehensive guide. Discover modern IP blocking techniques and practical solutions like residential proxies and CAPTCHA solvers.

web scraping
Logo of CapSolver

Lucas Mitchell

26-Jan-2026

Solve Captcha in Pydoll with CapSolver
How to Solve Captcha in Pydoll with CapSolver Integration

Learn how to solve reCAPTCHA and Cloudflare Turnstile in Pydoll using CapSolver for stealthy, async, CDP-based browser automation.

web scraping
Logo of CapSolver

Lucas Mitchell

23-Jan-2026

The Best API Search Companyโ€™s Homepage
The Best API Search Companyโ€™s Homepage: A Powerful Guide for Smarter Data Discovery

Evaluate the best api search company's homepage with our expert guide. Learn to assess technical transparency, developer experience, and core features for smarter data discovery and reliable API integration.

Logo of CapSolver

Nikolai Smirnov

23-Jan-2026

Top 10 No-Code Scrapers to Use in 2026
Top 10 No-Code Scrapers to Use in 2026

A curated list of the best no-code web scraping tools to use in 2026. Compare AI-powered scrapers, visual point-and-click platforms, pricing, pros and cons, and real-world use cases.

web scraping
Logo of CapSolver

Lucas Mitchell

21-Jan-2026

Maxun with CapSolver Integration
How to Solve Captcha in Maxun with CapSolver Integration

A practical guide to integrating CapSolver with Maxun for real-world web scraping. Learn how to handle reCAPTCHA, Cloudflare Turnstile, and CAPTCHA-protected sites using pre-auth and robot workflows.

web scraping
Logo of CapSolver

Ethan Collins

21-Jan-2026