CAPSOLVER
Blog
How to Change User Agent to Solve Cloudflare

How to Change User Agent to Solve Cloudflare

Logo of CapSolver

Anh Tuan

Data Science Expert

14-Jan-2025

Why User Agent Matters for passing Cloudflare

What Is a User Agent?

A User Agent (UA) is a string sent by your browser or client to identify itself to the server. It typically includes details like the browser name, version, operating system, and device type. For example:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36.

Cloudflare analyzes this string to detect bots. If your UA matches known automation tools (e.g., Python-Requests or HeadlessChrome), you’ll trigger Cloudflare’s anti-bot systems.


Step-by-Step: Changing Your User Agent

1. Identify the Target’s Expected User Agent

Before customizing, check what User Agents real users of the site are using. Tools like WhatIsMyBrowser or browser developer tools (Network tab > Headers) can help. For example, if the site is popular with Chrome users, mimic their UA.

2. Set a Custom User Agent in Your Code

Here’s how to modify the UA in popular programming languages:

Python (Requests Library)

python Copy
import requests

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
}

response = requests.get('https://example.com', headers=headers)

JavaScript (Node.js with Axios)

javascript Copy
const axios = require('axios');

axios.get('https://example.com', {
  headers: {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
  }
});

Using Headless Browsers (Puppeteer)

javascript Copy
const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36');
  await page.goto('https://example.com');
})();

3. Rotate User Agents

Using the same UA repeatedly can still flag you. Rotate UAs from a predefined list to mimic diverse users. Tools like Fake UserAgent simplify this:

python Copy
from fake_useragent import UserAgent
import requests

ua = UserAgent()
headers = {'User-Agent': ua.random}
response = requests.get('https://example.com', headers=headers)

Common Mistakes to Avoid

  1. Using Outdated User Agents
    Cloudflare maintains lists of suspicious UAs. Avoid strings tied to old browser versions (e.g., Chrome/58.0.3029.110 from 2017).

  2. Ignoring Headless Browser Fingerprints
    Even with a valid UA, headless browsers leak automation signals (e.g., missing plugins like navigator.plugins). Use stealth plugins like puppeteer-extra-plugin-stealth.

  3. Forgetting IP Rotation
    Pair UA rotation with residential proxies to avoid IP-based blocks. Static or sticky proxies work best for maintaining session consistency.

  4. Combine with TLS Fingerprinting
    Cloudflare checks TLS handshake patterns. Libraries like curl_cffi (Python) or tls-client (JavaScript) mimic real browser TLS fingerprints, reducing detection risk.


Final Thoughts

Changing your User Agent is a simple yet powerful way to bypass Cloudflare, but it’s not foolproof. Combine it with IP rotation, TLS fingerprinting, and anti-detection tools for robust results.

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

AI-powered Image Recognition: The Basics and How to Solve it
AI-powered Image Recognition: The Basics and How to Solve it

Say goodbye to image CAPTCHA struggles – CapSolver Vision Engine solves them fast, smart, and hassle-free!

Logo of CapSolver

Lucas Mitchell

24-Apr-2025

Best User Agents for Web Scraping & How to Use Them
Best User Agents for Web Scraping & How to Use Them

A guide to the best user agents for web scraping and their effective use to avoid detection. Explore the importance of user agents, types, and how to implement them for seamless and undetectable web scraping.

Logo of CapSolver

Ethan Collins

07-Mar-2025

What is a Captcha? Can Captcha Track You?
What is a Captcha? Can Captcha Track You?

Ever wondered what a CAPTCHA is and why websites make you solve them? Learn how CAPTCHAs work, whether they track you, and why they’re crucial for web security. Plus, discover how to bypass CAPTCHAs effortlessly with CapSolver for web scraping and automation.

Logo of CapSolver

Lucas Mitchell

05-Mar-2025

Cloudflare TLS Fingerprinting: What It Is and How to Solve It
Cloudflare TLS Fingerprinting: What It Is and How to Solve It

Learn about Cloudflare's use of TLS fingerprinting for security, how it detects and blocks bots, and explore effective methods to solve it for web scraping and automated browsing tasks.

Cloudflare
Logo of CapSolver

Lucas Mitchell

28-Feb-2025

Why do I keep getting asked to verify I'm not a robot?
Why do I keep getting asked to verify I'm not a robot?

Learn why Google prompts you to verify you're not a robot and explore solutions like using CapSolver’s API to solve CAPTCHA challenges efficiently.

Logo of CapSolver

Ethan Collins

27-Feb-2025

What is the best CAPTCHA solver in 2025
What is the best CAPTCHA solver in 2025

Discover the best CAPTCHA solver in 2025 with CapSolver, the ultimate tool for automated web scraping, CAPTCHA bypass, and data collection using advanced AI and machine learning. Enjoy bonus codes, seamless integration, and real-world examples to boost your scraping efficiency.

Logo of CapSolver

Aloísio Vítor

25-Feb-2025