MSDN フォーラムにあった質問「Edge向けWebDriverでDOMElementを取得するスクリプトを動かすと正しいJSONが戻らない」で、ExecuteScriptの戻り値でelement idが取得できることを知ったので、忘れないうちにメモしておきます。
using System;
using System.Collections.Generic;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
namespace EdgeSample
{
class Program
{
public static void Main(string[] args)
{
const string folderName = "Microsoft Web Driver";
string serverPath = System.IO.Path.Combine(
System.Environment.GetFolderPath(
System.Environment.SpecialFolder.ProgramFilesX86
), folderName
);
EdgeDriver driver = new EdgeDriver(serverPath);
driver.Navigate().GoToUrl("http://bing.com/");
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
wait.Until(x => x.Title.Contains("Bing"));
Dictionary<string, object> result = (Dictionary<string, object>)driver.ExecuteScript("return document.getElementById('sb_form_q');");
Console.WriteLine(result["id"]);
RemoteWebElement elm = (RemoteWebElement)driver.FindElementById("sb_form_q");
elm.SendKeys("Test"); //element idが上記'result["id"]'と一致
Console.ReadKey(true);
}
}
}




















この記事へのコメントはありません。