简体   繁体   English

无法使用带有 Java 的 Selenium2 (Webdriver) 启动 IE 浏览器

[英]Not able to launch IE browser using Selenium2 (Webdriver) with Java

I'm not able to launch IE browser to run my selenium automated tests written in Java.我无法启动 IE 浏览器来运行我用 Java 编写的 selenium 自动化测试。 I'm using Windows 7 and IE 9. Below is my code:我使用的是 Windows 7 和 IE 9。下面是我的代码:

Please find the attachment.请找到附件。在此处输入图片说明

public class GoogleNews {
    private WebDriver driver;
    private String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();
    ArrayList al = new ArrayList();
    @Before
    public void setUp() throws Exception {
        //driver = new FirefoxDriver();
        driver =new InternetExplorerDriver();
        baseUrl = "https://news.google.co.in/";

    }

    @Test
    public void testApple() throws Exception {
        driver.get(baseUrl);
    }
}

It needs to set same Security level in all zones.它需要在所有区域中设置相同的安全级别。 To do that follow the steps below:要做到这一点,请按照以下步骤操作:

  1. Open IE打开浏览器
  2. Go to Tools -> Internet Options -> Security转到工具 -> Internet 选项 -> 安全
  3. Set all zones (Internet, Local intranet, Trusted sites, Restricted sites) to the same protected mode, enabled or disabled should not matter.将所有区域(Int​​ernet、本地 Intranet、受信任站点、受限站点)设置为相同的保护模式,启用或禁用应该无关紧要。

Finally, set Zoom level to 100% by right clicking on the gear located at the top right corner and enabling the status-bar.最后,通过右键单击位于右上角的齿轮并启用状态栏,将缩放级别设置为 100%。 Default zoom level is now displayed at the lower right.默认缩放级别现在显示在右下方。

Well as the stack trace says, you would need to set the protected mode settings to same for all zones in IE.正如堆栈跟踪所说,您需要将 IE 中所有区域的保护模式设置设置为相同。 Read the why here : http://jimevansmusic.blogspot.in/2012/08/youre-doing-it-wrong-protected-mode-and.html在这里阅读原因: http : //jimevansmusic.blogspot.in/2012/08/youre-doing-it-wrong-protected-mode-and.html

and a quick how to from the same link : "In IE, from the Tools menu (or the gear icon in the toolbar in later versions), select "Internet options." Go to the Security tab. At the bottom of the dialog for each zone, you should see a check box labeled "Enable Protected Mode." Set the value of the check box to the same value, either checked or unchecked, for each zone"以及从同一链接快速操作的方法:“在 IE 中,从工具菜单(或更高版本工具栏中的齿轮图标),选择“Internet 选项”。转到“安全”选项卡。在对话框底部每个区域,您应该看到一个标记为“启用保护模式”的复选框。将复选框的值设置为相同的值,无论是选中还是未选中,对于每个区域”

The following snippet of WebDriver Java code should work to launch IE.以下 WebDriver Java 代码片段应该可以启动 IE。 The code will ignore the setup of Protected mode settings for all zone in the IE browser.该代码将忽略 IE 浏览器中所有区域的保护模式设置。

DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
File file = new File("D:\\Ripon Al Wasim\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
WebDriver driver = new InternetExplorerDriver(capabilities);
driver.get("https://www.google.com/");

Download IEDriverServer and extract it into your desired location and set the absolute path (In my above example it was "D:\\Ripon Al Wasim\\IEDriverServer.exe")下载 IEDriverServer 并将其解压缩到您想要的位置并设置绝对路径(在我上面的示例中,它是“D:\\Ripon Al Wasim\\IEDriverServer.exe”)

Rather than using Absolute path for IEDriverServer.exe, its better to use relative path in accordance to the project. IEDriverServer.exe 与其使用绝对路径,不如根据项目使用相对路径。

        DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
        capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
        File fil = new File("iDrivers\\IEDriverServer.exe");
        System.setProperty("webdriver.ie.driver", fil.getAbsolutePath());
        WebDriver driver = new InternetExplorerDriver(capabilities);        
        driver.get("https://www.irctc.co.in");          
  1. Go to IE->Tools->Internet Options .转到IE->Tools->Internet Options
  2. Go to Security tab.转到安全选项卡。
  3. Either Enable/Disable the protected mode for all(Internet, Local Intranet, Trusted Sites & Restricted Sites.)为所有人启用/禁用保护模式(Internet、本地 Intranet、受信任站点和受限站点。)

Wanted to share the actual code as few might still be confused about how to implement it.This is for C# NUNIT implementation.想要分享实际代码,因为很少有人可能仍然对如何实现它感到困惑。这是针对 C# NUNIT 实现的。 You need to do this because your company might not allow you to change the default security settings for obvious reasons.您需要这样做是因为您的公司可能出于显而易见的原因不允许您更改默认安全设置。 Good luck!祝你好运!

InternetExplorerOptions options = new InternetExplorerOptions();
            options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
            options.IgnoreZoomLevel = true;
            driver = new
                InternetExplorerDriver("C:\\Users\\stdd\\Desktop\\SLL\\SLLAutomation" +
                "\\Clysis\\STGSearch\\STGClaSearch\\Driver\\", options);
            driver.Manage().Window.Maximize();

For NighwatchJS use:对于 NightwatchJS 使用:

"ie" : {
  "desiredCapabilities": {
    "browserName": "internet explorer",
    "javascriptEnabled": true,
    "acceptSslCerts": true,
    "allowBlockedContent": true,
    "ignoreProtectedModeSettings": true
  }
},

To resolve this issue you have to do two things :要解决此问题,您必须做两件事:

  1. You will need to set a registry entry on the target computer so that the driver can maintain a connection to the instance of Internet Explorer it creates.您需要在目标计算机上设置一个注册表项,以便驱动程序可以保持与它创建的 Internet Explorer 实例的连接。

  2. Change few settings of Internet Explorer browser on that machine (where you desire to run automation).更改该机器上的 Internet Explorer 浏览器的一些设置(您希望在其中运行自动化)。

1 . 1 . Setting Registry Key / Entry :设置注册表项/条目:

  • To set registry key or entry, you need to open "Registry Editor".要设置注册表项或条目,您需要打开“注册表编辑器”。

  • To open "Registry Editor" press windows button key + r alphabet key which will open "Run Window" and then type "regedit" and press enter.要打开“注册表编辑器”,请按 windows 按钮键 + r 字母键,这将打开“运行窗口”,然后键入“regedit”并按 Enter。

  • Or Press Windows button key and enter "regedit" at start menu and press enter.或按 Windows 按钮键并在开始菜单中输入“regedit”并按 Enter。 Now depending upon your OS type whether 32/64 bit follow the corresponding steps.现在根据您的操作系统类型是否 32/64 位遵循相应的步骤。

Windows 32 bit : go to this location - "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Internet Explorer\\Main\\FeatureControl" and check for "FEATURE_BFCACHE" key. Windows 32 位:转到此位置 - “HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Internet Explorer\\Main\\FeatureControl”并检查“FEATURE_BFCACHE”键。

Windows 64 bit : go to this location - HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\Internet Explorer\\Main\\FeatureControl and check for "FEATURE_BFCACHE" key. Windows 64 位:转到此位置 - HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\Internet Explorer\\Main\\FeatureControl 并检查“FEATURE_BFCACHE”键。 Please note that the FEATURE_BFCACHE subkey may or may not be present, and should be created if it is not present.请注意FEATURE_BFCACHE子项可能存在也可能不存在,如果不存在则应创建

Important: Inside this key, create a DWORD value named iexplore.exe with the value of 0.重要提示:在此键中,创建一个名为 iexplore.exe 且值为 0 的 DWORD 值。

注册表设置

2 . 2 . Change Settings of Internet Explorer Browser :更改 Internet Explorer 浏览器的设置:

  • Click on setting button and select "Internet options" .单击设置按钮并选择“Internet 选项”

  • On "Internet options" window go to "Security" tab在“Internet 选项”窗口中,转到“安全”选项卡

  • Now select "Internet" option and unchecked the "Enable Protected Mode" check box and change the "Security level" to low.现在选择“Internet”选项并取消选中“启用保护模式”复选框并将“安全级别”更改为低。

  • Now select "Local Intranet" Option and change the "Security level" to low.现在选择“本地内联网”选项并将“安全级别”更改为低。

  • Now select "Trusted Sites" Option and change the "Security level" to low.现在选择“受信任的站点”选项并将“安全级别”更改为低。

互联网选项

  • Now click on "Apply" button , a warning pop up may appear click on "OK" button for warning and then on "OK" button on Internet Options window.现在单击“应用”按钮,可能会出现警告弹出单击“确定”按钮进行警告,然后单击“Internet 选项”窗口中的“确定”按钮。

保存设置

  • After this restart the browser.在此之后重新启动浏览器。

I was not able to modify the protected mode settings manually on my system since they were disabled.我无法在我的系统上手动修改保护模式设置,因为它们被禁用。 But the below VBA snippet for updating the registry values did the trick for me.(Please be cautious about any restrictions on your organization on modifying registry, before trying this)但是下面用于更新注册表值的 VBA 代码段对我有用。(在尝试之前,请注意您的组织对修改注册表的任何限制)

Const HKEY_CURRENT_USER = &H80000001
strComputer = "."

Set ScriptMe=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
    strComputer & "\root\default:StdRegProv")

'Disable protected mode for local intranet'
strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\1\"
strValueName = "2500"
dwValue = 0
ScriptMe.SetDWORDValue HKEY_CURRENT_USER,strKeyPath,strValueName,dwValue

'Disable protected mode for trusted pages'
strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\2\"
strValueName = "2500"
dwValue = 0
ScriptMe.SetDWORDValue HKEY_CURRENT_USER,strKeyPath,strValueName,dwValue

'Disable protected mode for internet'
strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\3\"
strValueName = "2500"
dwValue = 0
ScriptMe.SetDWORDValue HKEY_CURRENT_USER,strKeyPath,strValueName,dwValue

'Disable protected mode for restricted sites'
strKeyPath = "Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones\4\"
strValueName = "2500"
dwValue = 0
ScriptMe.SetDWORDValue HKEY_CURRENT_USER,strKeyPath,strValueName,dwValue

msgbox "Protected Mode Settings are updated"

Just copy paste the above code into notepad and save it with .vbs extension and double click it!只需将上面的代码复制粘贴到记事本中并使用 .vbs 扩展名保存并双击它!

Now try running your automation script again现在尝试再次运行您的自动化脚本

Before you start with Internet Explorer and Selenium Webdriver Consider these two important rules.开始使用 Internet Explorer 和 Selenium Webdriver 之前 请考虑这两个重要规则。

  • The zoom level :Should be set to default (100%) and缩放级别:应设置为默认值 (100%) 和
  • The security zone settings : Should be same for all.安全区域设置:所有人都应该相同。 The security settings should be set according to your organisation permissions.安全设置应根据您的组织权限进行设置。

How to set this?这个怎么设置?

  • Simply go to Internet explorer, do both the stuffs manually.只需转到 Internet Explorer,手动完成这两项操作。 Thats it.而已。 No secret.没有秘密。
  • Do it through your code.通过你的代码来做。

Method 1 :方法一

DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer(); DesiredCapabilities 能力 = DesiredCapabilities.internetExplorer();

    capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);

    System.setProperty("webdriver.ie.driver","D:\\IEDriverServer_Win32_2.33.0\\IEDriverServer.exe");

    WebDriver driver= new InternetExplorerDriver(capabilities);


    driver.get(baseURl);

    //Identify your elements and go ahead testing...

This will definetly not show any error and browser will open and also will navigate to the URL.这肯定不会显示任何错误,浏览器将打开并导航到 URL。

BUT This will not identify any element and hence you can not proceed.但是这不会识别任何元素,因此您无法继续。

Why?为什么? Because we have simly suppressed the error and asked IE to open and get that URL.因为我们只是简单地抑制了错误并要求 IE 打开并获取该 URL。 However Selenium will identify elements only if the browser zoom is 100% ie.然而,只有当浏览器缩放为 100% 时,Selenium 才会识别元素,即。 default.默认。 So the final code would be所以最终的代码是

Method 2 The robust and full proof way :方法 2 稳健且充分的证明方式

DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();

    capabilities.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, true);

    System.setProperty("webdriver.ie.driver","D:\\IEDriverServer_Win32_2.33.0\\IEDriverServer.exe");

    WebDriver driver= new InternetExplorerDriver(capabilities);


    driver.get(baseURl);

    driver.findElement(By.tagName("html")).sendKeys(Keys.chord(Keys.CONTROL,"0"));

    //Identify your elements and go ahead testing...

Hope this helps.希望这可以帮助。 Do let me know if further information is required.如果需要更多信息,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用Java使用Selenium Webdriver 3.0.1启动IE8浏览器 - Not able to launch IE8 browser using Selenium Webdriver 3.0.1 using Java 当浏览器窗口未聚焦时,使用Java的Selenium2(WebDriver)失败 - Selenium2 (WebDriver) using Java fails when browser window is not focused 无法使用 selenium webdriver 启动 chrome 浏览器 - Could not able to launch chrome browser using the selenium webdriver 使用Java的Selenium WebDriver(aka Selenium2)中的getPageSource() - getPageSource() in Selenium WebDriver(a.k.a Selenium2) using Java 如何使用Java在Selenium2(Webdriver)中键入Gmail正文文本 - How to type Gmail Body text in Selenium2 (Webdriver) using Java 如何在Selenium2(Webdriver)中处理POPUP浏览器? - How to Handle POPUP browser in Selenium2(Webdriver)? 无法使用 Selenium 和 WebDriver 启动浏览器 - Unable to launch a browser using Selenium and WebDriver Selenium webdriver无法在IE中启动应用程序,但在Firefox和chrome中打开 - Selenium webdriver Not able to launch application in IE, but opens in Firefox and chrome 无法为IE8浏览器添加Cookies(Java,Selenium Grid,WebDriver 2.41.0) - Not able to add Cookies for IE8 browser (Java, Selenium Grid, WebDriver 2.41.0) 我无法使用 Selenium 启动浏览器 - I am not able to launch the browser by using Selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM