1

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!

Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
Adam Zbudniewek
  • 107
  • 2
  • 12
  • Does this answer your question? [Can Selenium interact with an existing browser session?](https://stackoverflow.com/questions/8344776/can-selenium-interact-with-an-existing-browser-session) – Simon MᶜKenzie Mar 15 '22 at 21:01
  • 1
    No , because there are only examples in python . I need communicate via C# – Adam Zbudniewek Mar 16 '22 at 07:44
  • 1
    After a quick search, I found [Selenium: Attach to an Existing Chrome Browser with C#](https://intellitect.com/selenium-chrome-csharp/), which was written in July 2019. Seems pretty recent. It involves starting a Chrome process from a windows application, and setting the remote debugging address. Not an exact match, but it matches your problem in spirit. To be honest, if you get it working, post an answer to your own question with the code. – Greg Burghardt Mar 16 '22 at 11:39
  • When you said *"I have program in C# that have 100000 lines of code and there is no time to rewrite everything to C#."* did you mean you do not have time to rewrite it in **Python**? – Greg Burghardt Mar 16 '22 at 12:16

1 Answers1

7

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.

Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
  • @AdamZbudniewek: You know, I bet as soon as the python script ends Chrome will close down. You might need to change the Python code so it leaves the Chrome browser window open. Or better yet, just start Google Chrome from Python or C#. – Greg Burghardt Mar 18 '22 at 15:13
  • @AdamZbudniewek: Are you serious! It *actually* worked!? I'm glad it worked out. I'm not going to lie, I thought this solution would crash and burn. – Greg Burghardt Mar 18 '22 at 15:22