[英]How to get cookies on internet explorer using Selenium webdriver - C#
[英]When using Selenium Webdriver, I get the following error using the InternetExplorerDriver - “Unexpected error launching Internet Explorer…”
我正在嘗試在 C# 中實例化 InternetExplorerDriver,每次我都收到以下錯誤消息:
System.InvalidOperationException:啟動 Internet Explorer 時出現意外錯誤。 必須將所有區域的保護模式設置為相同的值(啟用或禁用)。 (NoSuchDriver)
現在我不確定如何解決這個問題,但觸發相關錯誤的代碼行是:
IWebDriver driver = new InternetExplorerDriver();
InternetExplorerDriver 的文檔建議我可以在重載的構造函數中傳入ICapabilities
object,但它只有屬性BrowserName
、 IsJavaScriptEnabled
、 Platform
和Version
。 這些似乎都沒有表明他們可以解決這個問題。
在實現中我需要做些什么來解決這個問題嗎? 還是我必須修改 IE9 本身的一些設置?
作為參考,如果您希望覆蓋這里的安全選項 c# 代碼:
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
namespace SeleniumTests
{
[TestFixture]
public class Test
{
private IWebDriver driver;
[SetUp]
public void Setup()
{
var options = new InternetExplorerOptions();
options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
driver = new InternetExplorerDriver(options);
}
}
}
更新:
我之前的答案使用了舊版本的 Selenium 2.0,我現在更新了代碼以針對 Selenium DotNet-2.21.0 工作並包含正確的命名空間。
Internet Explorer 定義了四個區域,每個區域具有不同的安全級別以及啟用或禁用保護模式的能力。 錯誤消息試圖告訴您,由於 Selenium 的InternetExplorerDriver
的限制,必須為所有區域禁用或啟用保護模式。
有關更多詳細信息,請參閱Selenium 問題跟蹤器中的缺陷報告和Internet Explorer 安全選項的屏幕截圖。
這應該可以解決問題:
InternetExplorerOptions options = new InternetExplorerOptions();
options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
IWebDriver driver = new InternetExplorerDriver(options);
Aleh 的回答為我解決了這個問題,但我發現我還需要為IEDriverServer
的位置指定文件路徑。 只是發布以防其他人遇到類似問題。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;
namespace SeleniumTest
{
class Program
{
static void Main(string[] args)
{
InternetExplorerOptions options = new InternetExplorerOptions();
options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
IWebDriver driver = new InternetExplorerDriver("C:\\Selenium", options);
driver.Navigate().GoToUrl("http://www.stackoverflow.com");
}
}
}
我發現以下內容對我有用(以上答案均無效)
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.SetCapability(InternetExplorerDriver.IntroduceInstabilityByIgnoringProtectedModeSettings, true);
driver = new InternetExplorerDriver(desiredCapabilities);
我發現它有效,但是當我嘗試運行測試時,我也收到了“(意外警報打開)”,結果證明我必須禁用 IE 開發人員工具欄。
如果您升級或降級在操作系統安裝期間安裝的本機 IE 瀏覽器,它將不允許打開勺子瀏覽器。 每次我們需要制作默認的 IE 瀏覽器時,它都會起作用。 假設當您安裝操作系統時,默認的 IE 版本是 IE8,而您出於某種目的升級到 IE9。 在這種情況下,它不允許在 Spoon 瀏覽器中導航任何應用程序(只有瀏覽器會打開),它會簡單地拋出錯誤消息,如“啟動 Internet Explorer IELaunchURL 錯誤時出現意外錯誤,返回 80070012”。
我在構建的服務器上遇到了類似的問題,我無法更改保護模式。 它被系統管理員禁用。 即使我使用管理員帳戶登錄,我也無法更改保護模式。 但是,我能夠毫無問題地運行 selenium。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.