ソースコード例
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using System;
using System.Collections.ObjectModel;
using System.IO;
namespace Selenium
{
/// <summary>
/// 横浜市営バス
/// </summary>
class ClassHamaBus
{
/// <summary>
/// 横浜市営バス系統図
/// </summary>
public void GetBusList()
{
// 画面を非表示
ChromeOptions options = new ChromeOptions();
options.AddArgument("--headless");
// Webドライバ
IWebDriver driver = new ChromeDriver(options);
// 暗黙の待機秒数設定
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
ReadOnlyCollection<IWebElement> busLineList;
ReadOnlyCollection<IWebElement> busLineRouteList;
ReadOnlyCollection<IWebElement> busLineStopList;
IWebElement a;
string path = "横浜市営バス系統一覧.txt";
try
{
// 乗換・時刻表検索ページ表示
driver.Navigate().GoToUrl("https://navi.hamabus.city.yokohama.lg.jp/koutuu/pc/map/Top?window=diagram");
// 画面の倍率設定
(driver as IJavaScriptExecutor).ExecuteScript("document.body.style.zoom= '75%';");
// ポップアップウィンドウ初回表示
if (driver.FindElements(By.Id("commentaryCheckbox")).Count > 0)
{
// この情報を次回から表示しない
driver.FindElement(By.Id("commentaryCheckbox")).Click();
// 閉じる
driver.FindElement(By.Id("commentarySubmit")).Click();
}
// 時刻表タブ選択
driver.FindElement(By.Id("tab-diagram")).Click();
// 系統から探す
driver.FindElement(By.XPath("//*[@id='divDiagramItem']/div[6]/ul/li[2]")).Click();
// 系統一覧
busLineList = driver.FindElements(By.CssSelector(".bus-line-name a"));
for (int busLineIndex = 0; busLineIndex < busLineList.Count; busLineIndex++)
{
busLineList = driver.FindElements(By.CssSelector(".bus-line-name a"));
IWebElement busLine = busLineList[busLineIndex];
File.AppendAllText(path, busLine.Text + "\r\n");
(driver as IJavaScriptExecutor).ExecuteScript("arguments[0].click();", busLine);
// 行先一覧
busLineRouteList = driver.FindElements(By.CssSelector(".bus-line-route-name a"));
for (int busLineRouteIndex = 0; busLineRouteIndex < busLineRouteList.Count; busLineRouteIndex++)
{
busLineRouteList = driver.FindElements(By.CssSelector(".bus-line-route-name a"));
IWebElement busLineRoute = busLineRouteList[busLineRouteIndex];
File.AppendAllText(path, "\t" + busLineRoute.Text + "\r\n");
(driver as IJavaScriptExecutor).ExecuteScript("arguments[0].click();", busLineRoute);
// 停留所一覧
busLineStopList = driver.FindElements(By.CssSelector(".bus-line-stop-name"));
for (int busLineStopIndex = 0; busLineStopIndex < busLineStopList.Count; busLineStopIndex++)
{
IWebElement busLineStop = busLineStopList[busLineStopIndex];
File.AppendAllText(path, "\t\t" + busLineStop.Text + "\r\n");
}
// 行先一覧へ戻る
a = driver.FindElements(By.CssSelector(".breadcrumb-title a"))[1];
(driver as IJavaScriptExecutor).ExecuteScript("arguments[0].click();", a);
}
// 系統一覧へ戻る
a = driver.FindElements(By.CssSelector(".breadcrumb-title a"))[0];
(driver as IJavaScriptExecutor).ExecuteScript("arguments[0].click();", a);
}
}
catch
{
throw;
}
}
}
}
ソースコード解説
画面の倍率設定
画面アクセス時、初回ポップアップ広告が表示されるが、画面の表示倍率によっては、領域外で「閉じる」ボタンがクリックできない。
スクロール、bodyタグに倍率を設定など、参考コードを色々試した結果、「(driver as IJavaScriptExecutor).ExecuteScript(“document.body.style.zoom= ‘X%’;”);」に落ち着いた。
JavaScriptコード
万が一要素が画面外の時、クリック動作が失敗する可能性を考慮し、ポップアップウィンドウ内でのクリック動作には、「要素.Click();」ではなく、「(driver as IJavaScriptExecutor).ExecuteScript(“arguments[0].click();”, 要素);」を使用した。
コメント