Created
April 21, 2020 20:55
-
-
Save AnderRasoVazquez/f1c571482e2aeb4f947c838f0875f00b to your computer and use it in GitHub Desktop.
Attach Selenium to an existing instance of Chrome in Windows or Linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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