Home > Enterprise >  C# start chromedriver from python and use it in c# like driver
C# start chromedriver from python and use it in c# like driver

Time:03-19

i need just to start chromedriver from python like this :

import undetected_chromedriver as uc
from selenium import webdriver

options = webdriver.ChromeOptions() 
driver = uc.Chrome()
driver.get('https://accounts.stockx.com/login')


print("Chrome started")

I have program in C# that have 100000 lines of code and there is no time to rewrite everything to C#.

I have to open chromedriver like i show code above. And i need to have acces to this chromedriver from C# code. How can i do that?

In C# i just do that like this :

ChromeDriver driver;
var chromeOptions = new ChromeOptions();
driver = new ChromeDriver(chromeDriverService,chromeOptions);

The question is : How can i start chromedriver via python and later acces this driver from c#? I need it , because chromedriver from python is undetectable to captchas. Thank you for advances!

CodePudding user response:

I think I might have a solution based on Selenium: Attach to an Existing Chrome Browser with C#, which connects a Google Chrome browser started by a WPF application to a web driver started in another process.

From the blog post:

MainWindow:

private void LaunchBrowser_Click(object sender, RoutedEventArgs e)
{
    Process proc = new Process();
    proc.StartInfo.FileName = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
    proc.StartInfo.Arguments = "https://www.intellitect.com/blog/ --new-window --remote-debugging-port=9222 --user-data-dir=C:\\Temp";
    proc.Start();
}

The [--remote-debugging-port argument] is critical. It tells Chrome which port to use for remote debugging...

...

ChromeOptions options = new ChromeOptions();
options.DebuggerAddress = "127.0.0.1:9222";

We point Selenium at a debugger address (port included). Now, we can “attach” to the Chrome instance launched by our desktop app.

This gives us the basic pattern to try in Python.

  1. (Python) Start ChromeDriver and specify the --remote-debugging-port in the arguments using ChromeOptions:

    options = webdriver.ChromeOptions()
    options.add_argument("--remote-debugging-port=9222")
    #                   Port number to use in C#: ^^^^ (change to suit your needs)
    driver = webdriver.Chrome(chrome_options=options)
    
  2. (C#) Initialize another ChromeDriver object, and specify the debugger address using the same port number as the --remote-debugging-port in Python:

    var options = new ChromeOptions()
    {
        DebuggerAddress = "127.0.0.1:9222"
        //  port number from Python: ^^^^
    };
    
    var driver = new ChromeDriver(options);
    

I haven't attempted this, but it follows the same basic steps. To be honest, it is a blind guess, so if it doesn't work, feel free to let me know or ask a follow-up question.

  • Related