LoginSignup
2
6

More than 1 year has passed since last update.

Windows 10 + C# + selenium で headless edge(Chromium) を試してみる

Last updated at Posted at 2020-06-18

目的

Edge(Chromium)をC# + selenium からキックしてみる
※Selenium4 はこちら Windows 10 + C# + Selenium4 で Chrome と Edge を headlessで起動してみる
OpenQA.Selenium.Chrome; を使用する版と使用しない版の2typeを試す
OpenQA.Selenium.Support.UI.ExpectedConditions method is now deprecatedに対しては
SeleniumExtras.WaitHelpers.ExpectedConditionsで書き換えて対応してみる
※サイトの作成された時期によって修正の量が違うと思われる・・・・
PythonでのWebDriverWaitは

    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.TagName, 'h3'))
    )

を書き換えると以下になるのかいな??

    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));            
    IWebElement firstResult = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(By.TagName("h3")));

追加パッケージ

.NETFramework 4.5でプロジェクトを作成後
プロジェクト -> NuGetパッケージの管理より以下を追加する


DotNetSeleniumExtras.WaitHelpers
Selenium.WebDriver
Microsoft.Edge.SeleniumTools

サンプルコード

Seleniumブラウザー自動化プロジェクトのCS版を書き換えてみる

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using SeleniumExtras.WaitHelpers;
using Microsoft.Edge.SeleniumTools;

//
// OpenQA.Selenium.Chrome; を使用する版
//
private void btnEdge_Click(object sender, EventArgs e)
{
    var options = new EdgeOptions();
    options.UseChromium = true;
    //options.AddArgument("headless");
    options.AddArgument("disable-gpu");
    options.BinaryLocation = @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";

    var service = ChromeDriverService.CreateDefaultService(@"C:\Dev\code\vs2019\scraping\Edge0001\Edge0001", "msedgedriver.exe");
    IWebDriver driver = new EdgeDriver(options);

    try
    {
        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));

        //Webページを開く
        driver.Navigate().GoToUrl("https://www.google.co.jp");

        //検索ボックスに検索ワードを入力
        IWebElement el = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(By.Name("q")));
        driver.FindElement(By.Name("q")).SendKeys("Selenium" + OpenQA.Selenium.Keys.Enter);

        //検索結果が出たという判定を何にするか?? は仕様依存??
        IWebElement elm = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementExists(By.TagName("h3")));

        //ページソースの出力
        Console.WriteLine(driver.PageSource);
    }
    catch (Exception)
    {
        Console.WriteLine("Err");
    }
    finally
    {
        if (driver != null)
        {
            driver.Quit();
        }
    }
}

//
// using OpenQA.Selenium.Chrome; を使用しない版
//
private void btnEdgeTool_Click(object sender, EventArgs e)
{
    var options = new EdgeOptions();
    options.AddArgument("headless");
    options.AddArgument("disable-gpu");
    options.BinaryLocation = @"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";

    var service = EdgeDriverService.CreateDefaultService(@"C:\Dev\code\vs2019\scraping\Edge0001\Edge0001", "msedgedriver.exe");
    IWebDriver driver = new EdgeDriver(service, options);

    try
    {
        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));

        //Webページを開く
        driver.Navigate().GoToUrl("https://www.google.co.jp");

        //検索ボックスに検索ワードを入力
        IWebElement el = wait.Until(ExpectedConditions.ElementExists(By.Name("q")));
        driver.FindElement(By.Name("q")).SendKeys("Selenium" + OpenQA.Selenium.Keys.Enter);

        //検索結果が出たという判定を何にするか?? は仕様依存??
        IWebElement elm = wait.Until(ExpectedConditions.ElementExists(By.TagName("h3")));

        //ページソースの出力
        Console.WriteLine(driver.PageSource);
    }
    catch (Exception)
    {
        Console.WriteLine("Err");
    }
    finally
    {
        if (driver != null)
        {
            driver.Quit();
        }
    }
}

参考にしたのは以下のサイト

Seleniumブラウザー自動化プロジェクト
C# Selenium 'ExpectedConditions is obsolete'
Selenium Web Driver, ExpectedConditions [deprecated] alternative
seleniumを使ってC#でGoogle Chromeの自動操作をする
【C#】Selenium ChromeDriverを使ってWebスクレイピング

2
6
0

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
6