Giter VIP home page Giter VIP logo

undetectedchromedriver's Introduction

UndetectedChromeDriver

This repo is C# implementation of undetected_chromedriver.

It optimizes Selenium chromedriver to avoid being detected by anti-bot services.


Nuget install

PM> Install-Package Selenium.UndetectedChromeDriver

Example

using SeleniumUndetectedChromeDriver;
using var driver = UndetectedChromeDriver.Create(
    driverExecutablePath: @"D:\xxx\chromedriver.exe");

driver.GoToUrl("https://nowsecure.nl");

If your chrome is not in default path, you can use browserExecutablePath to set.

using var driver = UndetectedChromeDriver.Create(
    ...
    browserExecutablePath: @"C:\xxx\chrome.exe");

Options example

var options = new ChromeOptions();
options.AddArgument("--mute-audio");

using var driver = UndetectedChromeDriver.Create(
    ...
    options: options);

Options cannot be shared, you need to create a new one every time.

var createOptions = () =>
{
    var options = new ChromeOptions();
    ...
    return options;
};
var driver1 = UndetectedChromeDriver.Create(
    options: createOptions());
var driver2 = UndetectedChromeDriver.Create(
    options: createOptions());

UserDataDir example

var userDataDir = @"D:\xxx\ChromeUserData";
using var driver = UndetectedChromeDriver.Create(
    ...
    userDataDir: userDataDir);

WPF example

public partial class MainWindow : Window
{
    private UndetectedChromeDriver _driver;
    public MainWindow()
    {
        InitializeComponent();
        this.Loaded += MainWindow_Loaded;
        this.Closed += MainWindow_Closed;
    }

    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        var driverExecutablePath = $@"D:\xxx\chromedriver.exe";

        _driver = UndetectedChromeDriver.Create(
            driverExecutablePath: driverExecutablePath,
            // hide selenium command prompt window  
            hideCommandPromptWindow: true);
    }

    private void MainWindow_Closed(object? sender, EventArgs e)
    {
        _driver.Dispose();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        _driver.GoToUrl("https://nowsecure.nl");
    }
}

Prefs example

// dict value can be value or json
var prefs = new Dictionary<string, object>
{
    ["download.default_directory"] =
        @"D:\xxx\download",
    ["profile.default_content_setting_values"] =
        Json.DeserializeData(@"
            {
                'notifications': 1
            }
        ")
};

using var driver = UndetectedChromeDriver.Create(
    ...
    prefs: prefs);

Multiple instance example

// options cannot be shared
var options1 = new ChromeOptions();
var options2 = new ChromeOptions();

// userDataDir cannot be shared
var userDataDir1 = @"D:\xxx\ChromeUserData1";
var userDataDir2 = @"D:\xxx\ChromeUserData2";

using var driver1 = UndetectedChromeDriver.Create(
    ...
    options: options1,
    userDataDir: userDataDir1);

using var driver2 = UndetectedChromeDriver.Create(
    ...
    options: options2,
    userDataDir: userDataDir2);

Parameters

  • options: ChromeOptions, optional, default: null
     Used to define browser behavior.

  • userDataDir: str, optional, default: null
     Set chrome user profile directory.
     creates a temporary profile if userDataDir is null,
     and automatically deletes it after exiting.

  • driverExecutablePath: str, required
     Set chrome driver executable file path. (patches new binary)

  • browserExecutablePath: str, optional, default: null
     Set browser executable file path.
     default using $PATH to execute.

  • logLevel: int, optional, default: 0
     Set chrome logLevel.

  • headless: bool, optional, default: false
     Specifies to use the browser in headless mode.
     warning: This reduces undetectability and is not fully supported.

  • suppressWelcome: bool, optional, default: true
     First launch using the welcome page.

  • hideCommandPromptWindow: bool, optional, default: false
     Hide selenium command prompt window.

  • commandTimeout: TimeSpan, optional, default: null
     The maximum amount of time to wait for each command.
     default value is 60 seconds.

  • prefs: Dictionary<string, object>, optional, default: null
     Prefs is meant to store lightweight state that reflects user preferences.
     dict value can be value or json.

  • configureService: Action<ChromeDriverService>, optional, default: null
     Initialize configuration ChromeDriverService.


Chrome argument example

// Set language.
options.AddArguments("--lang=en");
// Set screen.
options.AddArguments("--window-size=1920,1080");
options.AddArguments("--start-maximized");
// Enable geolocation.
var prefs = new Dictionary<string, object>
{
    ["profile.default_content_setting_values.geolocation"] = 1
};

using var driver = UndetectedChromeDriverCreate(
    ...
    prefs: prefs);
// Set geolocation.
driver.ExecuteCdpCommand(
    "Emulation.setGeolocationOverride",
    new Dictionary<string, object>
    {
        ["latitude"] = 42.1408845,
        ["longitude"] = -72.5033907,
        ["accuracy"] = 100
    });
// Set timezone.
driver.ExecuteCdpCommand(
    "Emulation.setTimezoneOverride",
    new Dictionary<string, object>
    {
        ["timezoneId"] = "America/New_York"
    });
// Disable WebGL.
options.AddArgument("--disable-gpu");
options.AddArgument("--disable-software-rasterizer");
// Disable WebRTC.
var prefs = new Dictionary<string, object>
{
    ["webrtc.ip_handling_policy"] = "disable_non_proxied_udp",
    ["webrtc.multiple_routes_enabled"] = false,
    ["webrtc.nonproxied_udp_enabled"] = false
};
using var driver = UndetectedChromeDriverCreate(
    ...
    prefs: prefs);
// Add Extensions.
var extension1 = @"D:\ChromeExtensions\Extension1";
var extension2 = @"D:\ChromeExtensions\Extension2";
options.AddArgument($"--load-extension={extension1},{extension2}");

Network capture

// import dev tools version
using DevToolsVer = OpenQA.Selenium.DevTools.V103;

// capture iframe
options.AddArgument("--disable-features=IsolateOrigins,site-per-process");

// log request url
var devTools = ((IDevTools)driver).GetDevToolsSession()
    .GetVersionSpecificDomains<DevToolsVer.DevToolsSessionDomains>();
await devTools.Network.Enable(new DevToolsVer.Network.EnableCommandSettings());
devTools.Network.RequestWillBeSent += (sender, e) =>
{
    Console.WriteLine(e.Request.Url);
};

Suppress initial information

using var driver = UndetectedChromeDriver.Create(
    ...
    configureService: (service) =>
    {
        // Suppress chrome driver initial information.
        service.SuppressInitialDiagnosticInformation = true;
    });

Get cookies

var cookies = string.Join("; ",
    driver.Manage().Cookies.AllCookies
        .Select(it => $"{it.Name}={it.Value}"));

Add cookies

driver.GoToUrl("url");

driver.Manage().Cookies.AddCookie(
    new OpenQA.Selenium.Cookie("TestCookie", "TestCookieValue"));

driver.GoToUrl("url");

Get user agent

var userAgent = driver.ExecuteJavaScript<string>(
    "return navigator.userAgent;");

Use proxy

options.AddArguments("--proxy-server=192.168.1.100:2048");
options.AddArguments("--proxy-server=socks5://192.168.1.100");

Move off screen

options.AddArgument(@"--window-position=""10000,10000""");

Declare

This repo is not an official undetected_chromedriver product.

undetectedchromedriver's People

Contributors

fysh711426 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.