Skip to content

Instantly share code, notes, and snippets.

@AnderRasoVazquez
Created April 21, 2020 20:55
Show Gist options
  • Save AnderRasoVazquez/f1c571482e2aeb4f947c838f0875f00b to your computer and use it in GitHub Desktop.
Save AnderRasoVazquez/f1c571482e2aeb4f947c838f0875f00b to your computer and use it in GitHub Desktop.
Attach Selenium to an existing instance of Chrome in Windows or Linux
using System;
using OpenQA.Selenium.Chrome;
using System.Diagnostics;
namespace selenium
{
class Program
{
static void Main(string[] args)
{
// Windows
// string programPath = @"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
// string dataDir = @"C:\\Temp"; // Windows
// Linux
string programPath = "/usr/bin/google-chrome";
string dataDir = "/tmp";
string port = "9222";
string url = @"http:\\www.reddit.com";
Process proc = new Process();
proc.StartInfo.FileName = programPath;
proc.StartInfo.Arguments = $@"{url} --new-window --remote-debugging-port={port} --user-data-dir={dataDir}";
proc.Start();
// Attach to Chrome
ChromeOptions options = new ChromeOptions();
options.DebuggerAddress = "127.0.0.1:9222";
ChromeDriver driver = new ChromeDriver(options);
driver.Navigate().GoToUrl(@"http:\\www.reddit.com");
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment