トップ回答者
Edge向けWebDriverでDOMElementを取得するスクリプトを動かすと正しいJSONが戻らない

質問
-
以下の条件にて確認。
Selenium Client 2.48.0
MicrosoftWebDriver.exe version 1.0
Microsoft Edge 20.10240.16284.0概要:
スクリプトを実行するexecuteにおいてElementを取得するスクリプトを実行すると、
Elementを記述するJSONデータがレスポンスに含まれるべきなのに、違うJSONデータがレスポンスに含まれる対処方法があるのであれば、お教えください?
詳細:
BingのWEBページに対して、
"return document.getElementById('sb_form_q');"
をWebDriverにて実行すると、
https://w3c.github.io/webdriver/webdriver-spec.html
にあるように、
{
"value": {"element-6066-11e4-a52e-4f735466cecf": id}
}
というJSONのレスポンスが戻るべきが、
{
"value": {"id": id}
}
が戻る。検証ソースコード:
-----
using OpenQA.Selenium;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
RemoteWebDriver driver = null;
string serverPath = "Microsoft Web Driver";
try
{
if (System.Environment.Is64BitOperatingSystem)
{
serverPath = Path.Combine(System.Environment.ExpandEnvironmentVariables("%ProgramFiles(x86)%"), serverPath);
}
else
{
serverPath = Path.Combine(System.Environment.ExpandEnvironmentVariables("%ProgramFiles%"), serverPath);
}// location for MicrosoftWebDriver.exe
EdgeOptions options = new EdgeOptions();
options.PageLoadStrategy = EdgePageLoadStrategy.Eager;
driver = new EdgeDriver(serverPath, options);//Set page load timeout to 5 seconds
driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(5));// Navigate to https://www.bing.com/
driver.Url = "https://www.bing.com/";WebDriverWait wait2 = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
wait2.Until(x => x.Title.Contains("Bing"));
object result = driver.ExecuteScript("return document.getElementById('sb_form_q');");
if (result != null)
{
if (result is Dictionary<string, object>)
{
Dictionary<string, object> dictionaryResult = (Dictionary<string, object>)result;Dictionary<string, object>.KeyCollection.Enumerator keysEnum = dictionaryResult.Keys.GetEnumerator();
while (keysEnum.MoveNext())
{
String key = keysEnum.Current;
object value = dictionaryResult[key];
Console.WriteLine("!!!! key= '" + key + "' value = '" + value.ToString() + "'");
}
}
else
{
Console.WriteLine("!!!!object ='" + result.ToString() + "'");
}
}
else
{
Console.WriteLine("!!!!! object is null");
}
// Find the search box and query for webdriver
RemoteWebElement element = (RemoteWebElement)driver.FindElementById("sb_form_q");
element.SendKeys("WebDriver");
element.SendKeys(Keys.Enter);
// Wait for search result
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
wait.Until(x => x.Title.Contains("WebDriver"));
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
if (driver != null)
{
driver.Close();
}
}
}
}
}
回答
-
こんにちは。
(例示された検証ソースコードは確認していないのですが…)
私の環境で確認したところ、executeを投げたときのレスポンスにはelement idがちゃんと含まれているようでした。{ "sessionId": "6B4FCF33-50AF-4AD4-9077-859B131D1B5A", "status": 0, "value": { "id": "0df1ceec-5da5-417c-a5cf-7df39230b763" } }
InternetExplorerDriverの場合だと下記のような形になるため、ブラウザー(WebDriver)によって若干の違いはあるようですが、どちらにせよ目的のelement idは取得できるかと思います。
{ "sessionId": "e7bad48f-1c3d-4567-b438-32b8189ae15b", "status": 0, "value": { "ELEMENT": "5730a0c2-e8cb-46cd-ba78-c4878094d8b1", "element-6066-11e4-a52e-4f735466cecf": "5730a0c2-e8cb-46cd-ba78-c4878094d8b1" } }
すべての返信
-
こんにちは。
(例示された検証ソースコードは確認していないのですが…)
私の環境で確認したところ、executeを投げたときのレスポンスにはelement idがちゃんと含まれているようでした。{ "sessionId": "6B4FCF33-50AF-4AD4-9077-859B131D1B5A", "status": 0, "value": { "id": "0df1ceec-5da5-417c-a5cf-7df39230b763" } }
InternetExplorerDriverの場合だと下記のような形になるため、ブラウザー(WebDriver)によって若干の違いはあるようですが、どちらにせよ目的のelement idは取得できるかと思います。
{ "sessionId": "e7bad48f-1c3d-4567-b438-32b8189ae15b", "status": 0, "value": { "ELEMENT": "5730a0c2-e8cb-46cd-ba78-c4878094d8b1", "element-6066-11e4-a52e-4f735466cecf": "5730a0c2-e8cb-46cd-ba78-c4878094d8b1" } }