我正在尝试在C#中实例化InternetExplorerDriver,每次我都会收到以下错误消息:
System.InvalidOperationException:启动Internet Explorer时出现意外错误。必须将保护模式设置为所有区域的相同值(启用或禁用)。 (NoSuchDriver)
现在我不确定如何解决这个问题,但触发错误的代码行是:
IWebDriver driver = new InternetExplorerDriver();
InternetExplorerDriver的文档建议我可以在重载的构造函数中传入ICapabilities
对象,但只包含属性BrowserName
,IsJavaScriptEnabled
,Platform
和{ {1}}。这些似乎都没有表明他们可以解决这个问题。
在实现中我需要做些什么才能解决这个问题? 或者我是否必须在IE9中修改某些设置?
答案 0 :(得分:26)
作为参考,如果你希望覆盖这里的安全选项是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,并包含了正确的命名空间。
答案 1 :(得分:17)
Internet Explorer定义了四个区域,每个区域具有不同的安全级别,并且能够启用或禁用保护模式。该错误消息试图告诉您,由于Selenium InternetExplorerDriver
的限制,必须为所有区域禁用或启用保护模式。
有关详细信息,请参阅defect report in Selenium's issue tracker和screenshot of Internet Explorer security options。
答案 2 :(得分:8)
这应该可以解决问题:
InternetExplorerOptions options = new InternetExplorerOptions();
options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
IWebDriver driver = new InternetExplorerDriver(options);
答案 3 :(得分:5)
Aleh's Answer为我解决了这个问题,但我发现我还需要指定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");
}
}
}
答案 4 :(得分:2)
我发现以下内容对我有效(上述答案均无效)
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.SetCapability(InternetExplorerDriver.IntroduceInstabilityByIgnoringProtectedModeSettings, true);
driver = new InternetExplorerDriver(desiredCapabilities);
我发现它有效,但当我尝试运行测试时,我也收到了“(意外的警报打开)”,其中有一点,我必须禁用IE开发人员工具栏。
答案 5 :(得分:2)
如果升级或降级在操作系统安装期间安装的本机IE浏览器,则不允许打开勺子浏览器。每当我们需要制作默认的IE浏览器时,它就会起作用。 假设您在安装操作系统时默认的IE版本是IE8,并且出于某种目的升级到IE9。在这种情况下,它将不允许在Spoon浏览器中导航任何应用程序(仅浏览器将打开),它只会抛出错误消息,如“意外错误启动Internet Explorer IELaunchURL错误返回80070012”。
答案 6 :(得分:1)
我在构建的服务器上遇到了类似的问题,我无法更改保护模式。它被系统管理员禁用。即使我使用管理员帐户登录,我也无法更改保护模式。但是,我能够毫无问题地运行硒。