LoginSignup
2
2

More than 1 year has passed since last update.

N番煎じですみませんが、SeleniumとC#でEdge操作する際の、初手メモ

Last updated at Posted at 2022-03-19

C#をスクラッチから書いて、コマンドラインでコンパイルするときのやり方です

1.webdriverを入手

手元のEdgeとバージョンと同じものを以下から取得

2.Seleniumを入手

の、右側

Download package から

https://www.nuget.org/api/v2/package/Selenium.WebDriver/4.1.0
selenium.webdriver.4.1.0.nupkg

を取得

(1)
得たファイルの拡張子をnupkgからzipにして中を開く
(2)
selenium.webdriver.4.1.0\lib の中から、対応するバージョンの
WebDriver.dll
を得る。

3.サンプルソースを、以下から得る

4.以下を同階層において、コンパイル

EdgeDriver_1.cs
msedgedriver.exe
WebDriver.dll

c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /reference:WebDriver.dll /target:exe EdgeDriver_1.cs

5.ソースは以下

(4.のMSサイトのサンプルをほぼママ使用してます)
(注:EdgeOptionsで、エラーを出なくしている)

EdgeDriver_1.cs
//c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe /reference:WebDriver.dll /target:exe EdgeDriver_1.cs

using OpenQA.Selenium;
using OpenQA.Selenium.Edge;
using System.Threading;

namespace EdgeDriver_1
{
	class Program
	{
		static void Main(string[] args)
		{
			var options = new EdgeOptions();
			options.AddExcludedArgument("enable-logging");
			var driver = new EdgeDriver(options);
			try
			{
				driver.Url = "https://bing.com";
				var element = driver.FindElement(By.Id("sb_form_q"));
				element.SendKeys("WebDriver");
				element.Submit();
				Thread.Sleep(1000);
			}
			finally
			{
				//driver.Quit();
			}
		}
	}
}
2
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2