seleniumbase (undetected Chrome driver): how to set request header? – Seleniumbase

by
Maya Patel
http-headers python selenium-webdriver seleniumbase

Quick Fix: Utilize seleniumwire’s header_overrides attribute to set desired request headers within your Selenium script.

The Problem:

While using seleniumbase with undetected Chrome driver (uc=True), how can I set the request header for specific web requests?

The Solutions:

Solution 1: Use seleniumwire for undetected Chrome driver and set header_overrides

Here’s a revised version of the solution:

You can utilize SeleniumWire to accomplish this. SeleniumWire is a wrapper around Selenium that extends it with additional features, including the ability to modify request headers. To set the request header using SeleniumWire with an undetected Chrome driver, follow these steps:

  1. Import the necessary modules:
from seleniumwire import webdriver
from selenium.webdriver.chrome.options import Options
from seleniumwire.undetected_chromedriver.v2 import Chrome, ChromeOptions
  1. Create an instance of ChromeOptions:
chrome_options = ChromeOptions()
  1. Start the Chrome driver with SeleniumWire:
driver = Chrome(seleniumwire_options={"options": chrome_options})
  1. Set the request header using the header_overrides attribute:
driver.header_overrides = {
    "Accept": "application/json",
}

That’s it! Now, when you make requests using the driver, the specified header will be automatically included in the request.

Solution 2: Via Javascript

A straightforward and effective solution for setting the request header in seleniumbase with Driver(uc=True) is to utilize Javascript. Here’s how you can achieve it:

  1. Import the necessary modules:
from seleniumbase import Driver
import io
  1. Initialize the seleniumbase driver with the uc=True option to enable undetected Chrome:
driver = Driver(uc=True)
  1. Perform any necessary login procedures to access the required website.

  2. Construct the JavaScript code to set the request header and make the HTTP request:

var callback = arguments[arguments.length - 1];
fetch('https://example.com', {method: 'GET', headers: {'Accept' : 'application/json'}}).then((response) => response.text().then((text) => callback({'status': response.status, 'text': text})))
  1. Execute the JavaScript code using the driver’s execute_async_script method:
response = driver.execute_async_script(javascript_code)
  1. Extract the response status and text from the returned JSON object:
status_code = response['status']
response_text = response['text']
  1. Handle the response based on the status code. For example, if the status code is 200 (indicating success), you can save the JSON content to a file:
if status_code == 200:
    with io.open(outfile, 'w', encoding='utf8', newline='\n') as f:
        f.write(response_text)
  1. Return the response status code.

This approach allows you to set custom request headers and make HTTP requests with the undetected Chrome driver provided by seleniumbase.

Q&A

How to set request header for seleniumbase undetected Chrome driver?

Use ChromeOptions and set header_overrides attribute with the desired header name and value.

Is there a simpler solution using javascript?

Yes, using execute_async_script method to set header and make a GET request.

Video Explanation:

The following video, titled "SeleniumBase Recorder Mode / Web Test Generator - YouTube", provides additional insights and in-depth exploration related to the topics discussed in this post.

Play video

... set a delay, but how to do it? 36:32. Go to channel · Undetectable automation with SeleniumBase UC Mode and undetected-chromedriver. (Python).