使用 Chrome 和 Selenium 设置 LocalStorage
Setting LocalStorage with Chrome and Selenium
我正在尝试使用 OpenQA Selenium 和 Chrome 设置本地存储键和值。我认为这将是相当微不足道的,但我似乎无法让它发挥作用。我对 C# 很陌生,所以我可能遗漏了一些东西。反正我有这个功能:
public static void SetMockData() { OpenQA.Selenium.Remote.RemoteWebStorage storage = new OpenQA.Selenium.Remote.RemoteWebStorage((OpenQA.Selenium.Remote.RemoteWebDriver)webDriver); storage.LocalStorage.SetItem("useTestData", "true"); }
我不确定这是否正确。无论如何,当我 运行 它时,我得到以下异常:
System.NotImplementedException : unknown command: Cannot call non W3C standard command while in W3C mode
但我终生无法关闭 W3C。我看不到要执行此操作的 chrome 命令参数,而且我似乎没有办法添加 chrome 选项。 ChromeOptions 中有一个名为 UseSpecCompliantProtocol
的选项,当设置为 false 或 true 时似乎没有任何作用。
在这种情况下,如何为 selenium 设置本地存储?
原来我用的代码是错的。在 this post
的帮助下
我得到以下正确的代码:
ILocalStorage webStorage = ((IHasWebStorage)webDriver).WebStorage.LocalStorage;
webStorage.SetItem("useTestData", "true");
然而,与 link 中的海报一样,这给了我一个例外:
System.InvalidOperationException: 'Driver does not support manipulating HTML5 web storage
所以网络驱动程序似乎不支持该功能。在 this post 的帮助下,我得到了以下代码:
IJavaScriptExecutor js = (IJavaScriptExecutor)webDriver;
js.ExecuteScript("localStorage.setItem('useTestData','true');");
这成功地将密钥添加到本地存储。添加密钥时要小心,因为它似乎与 URL 关联,因此如果在加载 URL 之前过早加载它,它将失败。
在 Visual Studio 中,当我向下钻取 ChromeDriver 基础 类 时,我最终到达了 OpenQA.Selenium.Webdriver。那里的许多属性已弃用,并带有一条有用的消息。例如:
[Obsolete("Use JavaScript instead for managing localStorage and sessionStorage. This property will be removed in a future release.")]
public bool HasWebStorage { get; }
[Obsolete("Use JavaScript instead for managing localStorage and sessionStorage. This property will be removed in a future release.")]
public IWebStorage WebStorage { get; }
[Obsolete("Use JavaScript instead for managing applicationCache. This property will be removed in a future release.")]
public bool HasApplicationCache { get; }
[Obsolete("Use JavaScript instead for managing applicationCache. This property will be removed in a future release.")]
public IApplicationCache ApplicationCache { get; }
[Obsolete("Getting and setting geolocation information is not supported by the W3C WebDriver Specification. This property will be removed in a future release.")]
public bool HasLocationContext { get; }
public ICommandExecutor CommandExecutor { get; }
[Obsolete("Getting and setting geolocation information is not supported by the W3C WebDriver Specification. This property will be removed in a future release.")]
public ILocationContext LocationContext { get; }
我正在尝试使用 OpenQA Selenium 和 Chrome 设置本地存储键和值。我认为这将是相当微不足道的,但我似乎无法让它发挥作用。我对 C# 很陌生,所以我可能遗漏了一些东西。反正我有这个功能:
public static void SetMockData() { OpenQA.Selenium.Remote.RemoteWebStorage storage = new OpenQA.Selenium.Remote.RemoteWebStorage((OpenQA.Selenium.Remote.RemoteWebDriver)webDriver); storage.LocalStorage.SetItem("useTestData", "true"); }
我不确定这是否正确。无论如何,当我 运行 它时,我得到以下异常:
System.NotImplementedException : unknown command: Cannot call non W3C standard command while in W3C mode
但我终生无法关闭 W3C。我看不到要执行此操作的 chrome 命令参数,而且我似乎没有办法添加 chrome 选项。 ChromeOptions 中有一个名为 UseSpecCompliantProtocol
的选项,当设置为 false 或 true 时似乎没有任何作用。
在这种情况下,如何为 selenium 设置本地存储?
原来我用的代码是错的。在 this post
的帮助下我得到以下正确的代码:
ILocalStorage webStorage = ((IHasWebStorage)webDriver).WebStorage.LocalStorage;
webStorage.SetItem("useTestData", "true");
然而,与 link 中的海报一样,这给了我一个例外:
System.InvalidOperationException: 'Driver does not support manipulating HTML5 web storage
所以网络驱动程序似乎不支持该功能。在 this post 的帮助下,我得到了以下代码:
IJavaScriptExecutor js = (IJavaScriptExecutor)webDriver;
js.ExecuteScript("localStorage.setItem('useTestData','true');");
这成功地将密钥添加到本地存储。添加密钥时要小心,因为它似乎与 URL 关联,因此如果在加载 URL 之前过早加载它,它将失败。
在 Visual Studio 中,当我向下钻取 ChromeDriver 基础 类 时,我最终到达了 OpenQA.Selenium.Webdriver。那里的许多属性已弃用,并带有一条有用的消息。例如:
[Obsolete("Use JavaScript instead for managing localStorage and sessionStorage. This property will be removed in a future release.")]
public bool HasWebStorage { get; }
[Obsolete("Use JavaScript instead for managing localStorage and sessionStorage. This property will be removed in a future release.")]
public IWebStorage WebStorage { get; }
[Obsolete("Use JavaScript instead for managing applicationCache. This property will be removed in a future release.")]
public bool HasApplicationCache { get; }
[Obsolete("Use JavaScript instead for managing applicationCache. This property will be removed in a future release.")]
public IApplicationCache ApplicationCache { get; }
[Obsolete("Getting and setting geolocation information is not supported by the W3C WebDriver Specification. This property will be removed in a future release.")]
public bool HasLocationContext { get; }
public ICommandExecutor CommandExecutor { get; }
[Obsolete("Getting and setting geolocation information is not supported by the W3C WebDriver Specification. This property will be removed in a future release.")]
public ILocationContext LocationContext { get; }