8000 Update README.md · solve-cloudflare/ruby-selenium@53530af · GitHub
[go: up one dir, main page]

Skip to content

Commit 53530af

Browse files
Update README.md
1 parent 78ceec9 commit 53530af

File tree

1 file changed

+245
-1
lines changed

1 file changed

+245
-1
lines changed

README.md

Lines changed: 245 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,245 @@
1-
# ruby-selenium
1+
# How to Solve CAPTCHA With Selenium in Ruby
2+
3+
![](https://assets.capsolver.com/prod/posts/captcha-selenium-ruby/D9v2aOgqJCOP-d2b5ca33bd970f64a6301fa75ae2eb22.png)
4+
5+
6+
CAPTCHAs, or Completely Automated Public Turing tests to tell Computers and Humans Apart, are designed to protect websites from automated bots. While they serve a crucial role in securing online platforms, they can be a significant hurdle when automating tasks with tools like Selenium. If you’re working with Selenium in Ruby and need to solve CAPTCHAs, this guide will provide a step-by-step approach to handling them effectively.
7+
8+
## What are Selenium and Ruby?
9+
10+
Before we dive into solving CAPTCHAs, it’s essential to understand the tools you’ll be working with: [Selenium](https://www.selenium.dev/) and Ruby.
11+
12+
- **Selenium** is a powerful open-source tool used for automating web browsers. It allows developers to write scripts in various programming languages to simulate user interactions with web pages, making it a popular choice for testing and web scraping.
13+
- **Ruby** is a dynamic, object-oriented programming language known for its simplicity and productivity. It’s often used in web development, and when combined with Selenium, it offers a robust framework for automating browser tasks.
14+
15+
16+
## Understanding CAPTCHAs and Their Types
17+
Before diving into the solution, it’s important to understand the different types of CAPTCHAs you might encounter:
18+
19+
- **ImageToText CAPTCHA**s: These require the user to enter characters displayed in a distorted image. You can find some common cases [here](https://docs.capsolver.com/en/guide/recognition/ImageToTextTask/)
20+
- **Image-based CAPTCHA**s: Users need to select images that match a given criterion (e.g., select all images with traffic lights). Mostly from reCAPTCHA
21+
- **reCAPTCHA**: Google's advanced CAPTCHA system that often requires recognizing objects in images or simply clicking a checkbox to prove you’re not a bot.
22+
![](https://assets.capsolver.com/prod/posts/captcha-selenium-ruby/BAn0JOEvTopm-d2b5ca33bd970f64a6301fa75ae2eb22.png)
23+
24+
- **hCAPTCHA**: Similar to reCAPTCHA, but often used by websites aiming for more privacy-focused solutions.
25+
26+
> Claim Your <u>**Bonus Code**</u> for top captcha solutions; [CapSolver](https://www.capsolver.com/?utm_source=official&utm_medium=blog&utm_campaign=seleniumruby): **WEBS**. After redeeming it, you will get an extra 5% bonus after each recharge, Unlimited
27+
>
28+
> ![](https://assets.capsolver.com/prod/images/post/2024-03-29/fbc29472-886c-45b2-9eb2-2b307f6d9700.png)
29+
>
30+
## Can Selenium Ruby Solve CAPTCHAs?
31+
32+
One of the most common questions among developers is whether Selenium with Ruby can solve CAPTCHAs. The short answer is: not directly. Selenium alone does not have built-in capabilities to solve CAPTCHAs because they are specifically designed to differentiate between human users and bots.
33+
However, there are several approaches to handling CAPTCHAs in Selenium Ruby:
34+
1. **Manual Intervention**: In some cases, developers manually solve the CAPTCHA during the automation process. However, this defeats the purpose of full automation.
35+
2. **Third-Party CAPTCHA Solvers:** The most effective method is integrating third-party services like [CapSolver](https://capsolver.com/?utm_source=official&utm_medium=blog&utm_campaign=seleniumruby) that specialize in solving CAPTCHAs using advanced algorithms and human intelligence.
36+
3. **Solving Simple CAPTCHAs**: For very basic text CAPTCHAs, developers might write custom scripts to recognize patterns, though this approach is limited and often unreliable.
37+
38+
While Selenium Ruby cannot solve CAPTCHAs on its own, with the right tools and services, it’s entirely possible to automate the process of bypassing CAPTCHAs, which we’ll explore in this guide.
39+
40+
## Setting Up Selenium in Ruby
41+
42+
43+
### Preparation
44+
45+
- **[Google Chrome](https://www.google.com/chrome/)**: Install the latest version of Chrome browser, as we will be using code to interact with Chrome.
46+
- **[Ruby](https://www.ruby-lang.org/)**: Ensure that Ruby is installed on your computer.
47+
- **[Selenium-webdriver](https://rubygems.org/gems/selenium-webdriver)**: The Ruby library for the automation tool Selenium.
48+
- **[CapSolver](https://docs.capsolver.com/)**: The official CapSolver documentation will help you solve CAPTCHAs.
49+
50+
Once Ruby is installed on your computer, you can install the Selenium WebDriver library by running the command `gem install selenium-webdriver`. Check your Chrome version, and based on that, download the corresponding `chromedriver.exe` driver. You can find the download links at the following locations:
51+
- **[Download link 1](https://googlechromelabs.github.io/chrome-for-testing/)**: Provides the latest Stable, Beta, Dev, and Canary versions of the driver.
52+
- **[Download link 2](https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json)**: Provides all drivers from version 113 onwards.
53 8000 +
- **[Download link 3](https://chromedriver.storage.googleapis.com/index.html)**: Provides all drivers for versions before 113.
54+
55+
### Analyzing the Target Website
56+
57+
We will use the website `https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php` as an example to solve reCAPTCHA using Ruby Selenium.
58+
59+
Before starting, we need to understand the basics of HTML form submission. By observing this page and opening the developer tools, we can manually solve the reCAPTCHA and then click the submit button. This action sends a POST request, submitting three fields: `ex-a`, `ex-b`, and `g-recaptcha-response`, as shown below:
60+
61+
![](https://assets.capsolver.com/prod/posts/captcha-selenium-ruby/iWhQaALv31g1-d2b5ca33bd970f64a6301fa75ae2eb22.png)
62+
63+
64+
These three fields correspond to two input elements and one textarea element under the form in the initial HTML source code, as shown below:
65+
66+
![](https://assets.capsolver.com/prod/posts/captcha-selenium-ruby/3AOAfGWFZHSg-d2b5ca33bd970f64a6301fa75ae2eb22.png)
67+
68+
69+
### Automating the Process with Ruby Selenium
70+
71+
How can we automate the entire process using Ruby Selenium? The steps are as follows:
72+
1. Ruby drives Selenium to visit the target website.
73+
2. Ruby calls the CapSolver API to solve the reCAPTCHA and obtain a token.
74+
3. Change the CSS style of the textarea element from `display: none` to `display: block` to make it interactive with Selenium.
75+
4. Simulate entering the token returned by CapSolver into the textarea element.
76+
5. Simulate clicking the submit button to submit the form and complete the verification.
77+
78+
### Visiting the Target Website with Ruby Selenium
79+
80+
Ensure that you replace the `driver_path` in the code below with the actual path to `chromedriver` on your computer.
81+
82+
```ruby
83+
require 'selenium-webdriver'
84+
85+
# Initialize Chrome browser options and access the target website
86+
driver_path = "path/to/chromedriver.exe"
87+
options = Selenium::WebDriver::Chrome::Options.new
88+
service = Selenium::WebDriver::Service.chrome(path: driver_path)
89+
driver = Selenium::WebDriver.for :chrome, options: options, service: service
90+
url = "https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php"
91+
driver.navigate.to url
92+
```
93+
94+
### Getting the Token
95+
96+
To use the CapSolver API, we need to provide the `websiteKey`, which can be found by searching for the keyword `data-sitekey` in the page source:
97+
98+
![](https://assets.capsolver.com/prod/posts/captcha-selenium-ruby/WNvXZd2JNMgT-d2b5ca33bd970f64a6301fa75ae2eb22.png)
99+
100+
101+
Now, let's write the Ruby code to use the CapSolver API to automatically solve the reCAPTCHA:
102+
103+
```ruby
104+
require 'net/http'
105+
require 'json'
106+
require 'time'
107+
108+
def cap_solver(api_key, public_key, page_url)
109+
payload = {
110+
"clientKey" => api_key,
111+
"task" => {
112+
"type" => 'ReCaptchaV2TaskProxyLess',
113+
"websiteKey" => public_key,
114+
"websiteURL" => page_url,
115+
}
116+
}
117+
118+
# Send a task creation request
119+
uri = URI("https://api.capsolver.com/createTask")
120+
res = Net::HTTP.post(uri, payload.to_json, { "Content-Type" => "application/json" })
121+
resp = JSON.parse(res.body)
122+
task_id = resp["taskId"]
123+
124+
unless task_id
125+
puts "Failed to create task: #{res.body}"
126+
return
127+
end
128+
129+
puts "Got taskId: #{task_id}"
130+
131+
# Loop waiting to obtain task results
132+
loop do
133+
sleep(1)
134+
payload = { "clientKey" => api_key, "taskId" => task_id }
135+
uri = URI("https://api.capsolver.com/getTaskResult")
136+
res = Net::HTTP.post(uri, payload.to_json, { "Content-Type" => "application/json" })
137+
resp = JSON.parse(res.body)
138+
status = resp["status"]
139+
if status == "ready"
140+
token = resp.dig("solution", "gRecaptchaResponse")
141+
puts "Solve succeed, token: #{token}"
142+
return token
143+
elsif status == "processing"
144+
puts "Solve in progress..."
145+
elsif status == "failed"
146+
puts "Solve failed! response: #{res.body}"
147+
return
148+
end
149+
end
150+
end
151+
```
152+
153+
### Using the Token in Selenium
154+
155+
Next, we need to input the token into the webpage, automatically click submit, and complete the entire process. Let's combine all the code; the complete code is as follows (be sure to replace `cap_solver_api_key` with your own key, which can be found in the CapSolver dashboard):
156+
157+
```ruby
158+
require 'selenium-webdriver'
159+
require 'net/http'
160+
require 'json'
161+
require 'time'
162+
163+
def cap_solver(api_key, website_key, page_url)
164+
payload = {
165+
"clientKey" => api_key,
166+
"task" => {
167+
"type" => 'ReCaptchaV2TaskProxyLess',
168+
"websiteKey" => website_key,
169+
"websiteURL" => page_url,
170+
}
171+
}
172+
173+
# Send a task creation request
174+
uri = URI("https://api.capsolver.com/createTask")
175+
res = Net::HTTP.post(uri, payload.to_json, { "Content-Type" => "application/json" })
176+
resp = JSON.parse(res.body)
177+
task_id = resp["taskId"]
178+
179+
unless task_id
180+
puts "Failed to create task: #{res.body}"
181+
return
182+
end
183+
184+
puts "Got taskId: #{task_id}"
185+
186+
# Loop waiting to obtain task results
187+
loop do
188+
sleep(1)
189+
payload = { "clientKey" => api_key, "taskId" => task_id }
190+
uri = URI("https://api.capsolver.com/getTaskResult")
191+
res = Net::HTTP.post(uri, payload.to_json, { "Content-Type" => "application/json" })
192+
resp = JSON.parse(res.body)
193+
status = resp["status"]
194+
if status == "ready"
195+
token = resp.dig("solution", "gRecaptchaResponse")
196+
puts "Solve succeed, token: #{token}"
197+
return token
198+
elsif status == "processing"
199+
puts "Solve in progress..."
200+
elsif status == "failed"
201+
puts "Solve failed! response: #{res.body}"
202+
return
203+
end
204+
end
205+
end
206+
207+
# Initialize Chrome browser options and access the target website
208+
driver_path = "path/to/chromedriver.exe"
209+
options = Selenium::WebDriver::Chrome::Options.new
210+
service = Selenium::WebDriver::Service.chrome(path: driver_path)
211+
driver = Selenium::WebDriver.for :chrome, options: options, service: service
212+
url = "https://recaptcha-demo.appspot.com/recaptcha-v2-checkbox.php"
213+
driver.navigate.to url
214+
215+
# Call CapSolver API to solve ReCaptcha
216+
cap_solver_api_key = 'YOUR_API_KEY'
217+
website_key = '6LfW6wATAAAAAHLqO2pb8bDBahxlMxNdo9g947u9'
218+
token = cap_solver(cap_solver_api_key, website_key, url)
219+
if token.nil? || token.empty?
220+
puts "Failed to solve captcha, Press any key to exit."
221+
STDIN.gets
222+
driver.quit
223+
return
224+
end
225+
226+
# Change the display style property of textarea to block to make it visible
227+
driver.execute_script("document.getElementById('g-recaptcha-response').style.display = 'block';")
228+
# Simulate inputting token into textarea
229+
textarea = driver.find_element(id: 'g-recaptcha-response')
230+
textarea.send_keys(token)
231+
# Simulate clicking and submitting a form
232+
submit_btn = driver.find_element(css: "button[type='submit']")
233+
submit_btn.click
234+
235+
puts "Press any key to exit."
236+
STDIN.gets
237+
driver.quit
238+
```
239+
![](https://assets.capsolver.com/prod/posts/captcha-selenium-ruby/acNrz22gjMbB-d2b5ca33bd970f64a6301fa75ae2eb22.png)
240+
241+
Run the above code and you will see that the recaptcha has been successfully solved.
242+
243+
## More Information
244+
245+
[CapSolver](https://www.capsolver.com/?utm_source=official&utm_medium=blog&utm_campaign=seleniumruby) uses AI-based automatic web unlock technology to help you solve CAPTCHAs in seconds. It can solve not only reCAPTCHA but also hCaptcha, Geetest, Cloudflare Turnstile, DataDome, AWS WAF, and more. CapSolver also provides SDKs in multiple languages as well as browser extensions. You can refer to the [CapSolver documentation](https://docs.capsolver.com/?utm_source=official&utm_medium=blog&utm_campaign=seleniumruby) for more information.

0 commit comments

Comments
 (0)
0