How do I establish an open connection to an open web browser in C#?

How do I establish an open connection to an open web browser in C#?

In a Microsoft C# program using a Visual Studio Code I am using the following namespaces:

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;

After I get a string variable, “url”, assigned with a URL of a website page, I use the following commands which successfully opens a browser window and loads the web site:

IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl(url);

But this opens a chrome browser which is denoted somewhere as being a “test” session. It somehow knows it was launched from a program. And, since I am using this program to automate some interactions with linkedin, this information is passed along to linkedin which prompts me that it requires I login. This creates a cascading seriies of events that are difficult to automate including using my cell as a means of verification.

Here, let me show you what I am up against. This screen shot:
https://www.likablelogic.org/images/Direct/01.png
shows that the web browser that my code launches through selenium commands has a label on it, “Chrome is bring controlled by automated test software”. Yes, and I wrote this automated test software.

What is most annoying is the fact that there is no log in to the session which is done automatically when I normally launch a browser. Here see this screen shot
https://www.likablelogic.org/images/Direct/02.png
where you can see that the user icon is this nondescript non-person.
I made a video to demonstrate how pointless and frusturating this is:

Youtube Video https://www.youtube.com/watch?v=-mgq7UHORmU

So, instead of taking this route, how do I establish an open connection to an open web browser in C#? I figure, if I instead connect to a web browser that is already open and already has its veriication steps done with linkedin, then I won’t be prompted to log in and do any user verification.

On a personal note, if this is intentional security measures to prevent people from abusing a system, then this is a sad thing.

On a broader view, will all this mean I will have to make a web browser from scratch?

I do not think so. Here is the bottom line. I am using selenium commands. Selenium is used for software testing. So there must be some way to attack a C# program to an actively open chrome web browser and interact with it. How do I do that?

Is it better to use a different browser other than Chrome? How would I do that in C# code?

So you want a program in C# to be able to take control of a user’s web browser, without their approval, and have full access to their logged in sessions.

Do… you perhaps see a security problem with this?

(Incidentally, you might want to go and read LinkedIn’s Terms of Service, because you’re violating 8.2.13.)

I found a solution, but unfortunately no in C#, but in Python.

https://github.com/ultrafunkamsterdam/undetected-chromedriver

pip install undetected-chromedriver

Then in your python code:

import undetected_chromedriver as uc
driver = uc.Chrome(headless=False,use_subprocess=False)

That’s it.

Call your selenium stuff as usual.

ps: Use responsibly

Apparently there is C# port

https://www.nuget.org/packages/Selenium.WebDriver.UndetectedChromeDriver

using Selenium.WebDriver.UndetectedChromeDriver;

var webBrowser = Selenium.WebDriver.UndetectedChromeDriver.UndetectedChromeDriver.Instance(false);
webBrowser.GoTo("https://google.com");

I don’t use C#, you will have to test it yourself

That terminology seems strange to me. I will assume you mean that you want to create a browser window or tab.

Okay yes I think you are saying you want to create a browser window.

But it is a browser window, therefore you are asking about something more than creation of a window or tab.

Now we are getting somewhere. What you are actually asking is how to do a login using Selenium.

You provided the answer yourself. The code you provided does that. I think you are trying to ask something else.

I think that is closer to what you are trying to ask. I apologize for being picky, but if I am wrong then I think some clarification would help. Yes, definitely if you can connect with a browser window or tab that has already had the security satisfied then you are likely in business. As I recall, it is possible to inject JavaScript into a browser window or tab. It has been a few years since I have written a program that does that but I could find my code if I wanted to. But first you need to find a way to connect with such a window or tab.

You might be able to use an extension for what you are trying to do.

Do you think that keeping malware out is sad or allowing it in is sad?

When I researched that a few years ago I could not find anything that I could use.

Great question! To establish an open connection with a web browser in C#, one common method is using the Process class to launch and control browser instances. You can then interact with the browser through COM automation for more complex tasks, like controlling navigation or extracting data. Has anyone used libraries like Selenium WebDriver or CefSharp for browser automation? Would love to hear about your experiences!

If you know how to get Selenium to access a browser session created that way then please share. My guess is that it is not obvious. Actually, my guess is that if it is even possible then it won’t solve the problem described here (a chrome browser which is denoted somewhere as being a “test” session).

Yeah I have done that. I have even written an article available in a couple of places. That works for IE but are you sure it works with Microsoft Edge?

I believe you are referring to using SHDocVw for the COM automation. I forget the difficulties I had doing that but it is not easy to figure out. If you have details of how to do that then I think wm_m_thompson would need that.

Instead the solution is typically to use WebView or WebView2 as in How to migrate from SHDocVw.DLL to WebView2 to read content from application url in edge? - Microsoft Q&A. I am not sure that those controls can be used with Selenium but perhaps that is possible. If so then that might work.