PCにインストールされているブラウザをC#で自動操縦する
PCにインストールされているブラウザをC#で自動操縦する
まずは、Windowsなのだから、IEはインストールされているとして、
が、インストールされているかを確認する。
方法は、ぐぐった結果レジストリの
HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\
に、[firefox.exe]、[chrome.exe]があれば、インストールされていると判断する。
後は、Nugetで
・Selenium WebDriver
・Selenium WebDriver Support Classes
・Selenium.WebDriver.ChromeDriver
・Selenium.WebDriver.IEDriver
をインストールする。
早速
IWebDriver driver = new ChromeDriver();
driver.Url = "www.example.com";
と、やるとChromeが起動して、指定したURLにアクセスする。
だけど・・・・・
ログを吐くようなコマンドプロンプトのウィンドウも出てくる。
オブジェクトブラウザで見て、
>|cs|
InternetExplorerOptions ieOptions = new InternetExplorerOptions();
InternetExplorerDriverService ieDriverService = InternetExplorerDriverService.CreateDefaultService();
ieDriverService.HideCommandPromptWindow = true;
driver = new InternetExplorerDriver(ieDriverService, ieOptions);
||<
だと出ない
>|cs|
ChromeOptions chromeOptions = new ChromeOptions();
ChromeDriverService chromeService = ChromeDriverService.CreateDefaultService();
chromeService.HideCommandPromptWindow = true;
driver = new ChromeDriver(chromeService, chromeOptions);
||<
で、出ないようになった。
ブラウザに表示されるフォームに自由に値を設定したり、
ボタンをクリックしたり、javascriptの起動と何でも出来る。
これで遠隔操作でもされると、マズいね。