Selenium 简明教程

Selenium - IE Driver

Internet Explorer 是一个独立的服务器,它实现了 Webdriver 的协议。已通过 Internet Explorer 11 版本和 Windows 10 验证了这一点。它可能适用于 Internet Explorer 和 Windows 的早期版本,但未得到官方支持。

The Internet Explorer is an independent server which implements the Webdriver’s wire protocol. This has been verified with the internet explorer 11 version and in Windows 10. It might work with the previous versions of internet explorer and Windows, however it is not officially supported.

该驱动程序为 32 位和 64 位浏览器版本提供支持。例如,如果使用 IEDriverServer.exe 的 32 位版本,则将打开 32 位版本的 Internet Explorer。同样,如果使用 IEDriverServer.exe 的 64 位版本,则将打开 64 位版本的 Internet Explorer。

The driver provides support to the 32 and 64 bits browser versions. For example, if the 32 version of the IEDriverServer.exe is used, then the internet explorer with the 32 bit version shall be opened. Similarly, if the 64 version of the IEDriverServer.exe is used, then the internet explorer with the 64 bit version shall be opened.

IE Driver Installation

在使用 InternetExplorerDriver 之前无需执行任何安装程序,但可能需要某些配置。独立的服务器可执行文件可通过图片 https://www.selenium.dev/downloads/ 中突出显示的部分的以下链接获取。

There is no prerequisite to execute any installer prior using the InternetExplorerDriver, however some configurations may be needed. The standalone server executable is available from the below link in the highlighted section of the image https://www.selenium.dev/downloads/.

selenium ie driver

下载后,应将其设置在正确的路径中。

Once it has been downloaded, it should be set in the correct path.

Advantages of IE Driver

InternetExplorerDriver 在真实浏览器中执行,并与 JavaScript 协作。

InternetExplorerDriver executes in real browsers and works with JavaScript.

Disadvantages of IE Driver

InternetExplorerDriver 仅适用于 Windows,并且速度较慢。

InternetExplorerDriver is only applicable to Windows and is on a slower side.

What are Command Line Switches?

可以通过借助多个命令行参数来更新 InternetExplorerDriver 的特征。

The characteristics of InternetExplorerDriver can be updated by taking help of multiple command-line arguments.

Switch

Purpose

–port=<portNumber>

Points to the port on which the HTTP server of the InternetExplorerDriver communicates with the commands from the language bindings. Default value is 5555.

–host=<hostAdapterIPAddress>

Points to the IPAddress of the host adapter on which the HTTP server of the InternetExplorerDriver communicates with the commands from the language bindings. Default value is 127.0.0.1.

–log-level=<logLevel>

Points to the level at which the logging messages are generated. Correct values are FATAL, ERROR, WARN, INFO, DEBUG, and TRACE. Default value is FATAL.

–log-file=<logFile>

Points to the complete path and file name of the log file. Default value is stdout.

–extract-path=<path>

Points to the complete path of the directory for extracting the support files utilized by the server. Default value is TEMP, if not mentioned.

–silent

Suppresses the diagnostic output when the server is put on.

What are Vital System Properties?

系统属性是使用 Java 中的 System.setProperty() 配置的,或使用 InternetExplorerDriver 利用的 -DpropertyName=value 命令行标志配置的。

The system properties are configured using the System.setProperty() in Java or by using -DpropertyName=value command line flag utilized by the InternetExplorerDriver.

Switch

Purpose

webdriver.ie.driver

Points to the location of the IE driver binary.

webdriver.ie.driver.host

Points to the IPAddress of the host adapter on which the the InternetExplorerDriver communicates with the commands from the language bindings.

webdriver.ie.driver.loglevel

Points to the level at which the logging messages are generated. Correct values are FATAL, ERROR, WARN, INFO, DEBUG, and TRACE. Default value is FATAL.

webdriver.ie.driver.logfile

Points to the complete path and file name of the log file.

webdriver.ie.driver.silent

Suppresses the diagnostic output when the server is put on.

webdriver.ie.driver.extractpath

Points to the complete path of the directory for extracting the support files utilized by the server. Default value is TEMP, if not mentioned.

Prerequisites for IE Driver Configurations

  1. The IEDriverServer should be downloaded from the link https://www.selenium.dev/downloads/

  2. Once it has been downloaded, it should be set in the correct path.

  3. On internet explorer version 7, higher versions of Windows Vista, Windows 7, and Windows 10, the Protected mode should be set the same for all zones. The value can be turned on and off provided it bears the same value for every zone. To set the Protected Mode, select Internet Options from the Tools menu and navigate to the Security Tab. For every zone, there is a checkbox at the end of the tab called the Enable Protected Mode.

  4. The Enhanced Protected Mode available in the Advanced tab of the Internet Options dialog, should be disabled for internet explorer version 10 and above.

  5. The zoom of the browser should be set to 100% which helps the native mouse events to the proper coordinates.

  6. The parameter Change the size of text, apps, and other items should be set to 100% for internet explorer version 10.

  7. We should set the registry entry on the target computer so that the driver can have a connection to the internet explorer object. For 32 bit windows, the key value should be −

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer
   \Main\FeatureControl\FEATURE_BFCACHE

对于 64 位 Windows,键值应为:

For 64 bit windows, the key value should be −

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft
   \Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE

请注意,如果子键 FEATURE_BFCACHE 不存在,则应创建它。此外,在该键中生成一个 DWORD 值,称为 iexplore.exe,并将其设为 0。

It should be noted that the subkey FEATURE_BFCACHE, should be created if not available. Also within this key, generate a DWORD value called the iexplore.exe set to 0.

What are Native Events and Internet Explorer?

由于 InternetExplorerDriver 仅在 Windows 上运行,因此它尝试利用所谓的本机或操作系统级事件,对浏览器执行鼠标和键盘操作。这与利用模拟的 JavaScript 事件执行相同操作的情况相反。

Since the InternetExplorerDriver runs on Windows only, it tries to utilize the so-called native or OS level events to do mouse and keyboard actions on the browsers. This is in contrast to utilizing the simulated JavaScript events for the same operations.

使用 JavaScript Events 的优点是不需要 JavaScript 沙盒,且它会确认浏览器中的正确 JavaScript 事件传播。但目前在 IE 浏览器没有获得焦点以及尝试悬停在元素上的情况下,鼠标事件存在一些问题。

The pros of using the JavaScript Events is that it does not require the JavaScript sandbox and it confirms correct JavaScript event propagation inside the browser. But presently there are few problems with mouse events when the IE browser does not have focus and while trying to hover on elements.

Browser Focus

问题在于,当 IE 浏览器失去焦点时,它并不完全支持发送到浏览器窗口的 Windows 消息。应该对唯一被点击的元素设置焦点窗口,但元素不会执行点击。

The issue is that the IE browser does not fully support the Windows messages that are sent to the browser window when it does not have focus. The only element being clicked should have a focus window but the click will not be undergone by the element.

Hovering Over Elements

在尝试悬停在元素上的过程中,物理鼠标位于 IE 浏览器窗口的边界,但悬停不起作用。它似乎会在很短的时间内起作用,但元素随后又会返回到其先前状态。

While trying to hover over elements, the physical mouse is at the boundary of the IE browser window, but the hover does not work. It appears to work for a very small time but then again the element moves back to its previous state.

Clicking <option> Elements or Submit Forms and alert()

有时候,IEDriver 不会使用本机事件与元素通信,例如在 <select> 标记中选择 <option> 元素时。此外,使用 submit()、alert()、prompt() 方法时也会遇到相同的问题。

Sometimes the IEDriver does not communicate with elements using the native events, for example while selecting the <option> elements within the <select> tag. Besides, the same issue is faced while using the submit(), alert(), prompt() methods.

Multiple Instances of InternetExplorerDriver

IEDriverServer.exe 允许创建 InternetExplorerDriver 的多个对象。但此功能尚未经过全面测试,且可能存在焦点、Cookie 等方面的问题。因此,建议改为使用虚拟机和 RemoteWebDriver。与多个 Internet Explorer 对象共享 Cookie 有两种解决方案。第一步是在隐私模式下启动 Internet Explorer。

The IEDriverServer.exe allows creation of more than one object of the InternetExplorerDriver. But this feature is not yet thoroughly tested and may have issues on focus, cookies etc. So it is recommended to use the virtual machines and RemoteWebDriver instead. There are two solutions related to cookies shared among the more than one objects of the internet explorer. The first step is to launch the internet explorer in private mode.

此后,Internet Explorer 将使用清理后的会话数据打开,且不会在终止时存储修改后的会话数据。要实现此目的,传递功能 - orceCreateProcessApi,其值为 true,以及浏览器命令行开关,其值为 -private。这仅适用于 Internet Explorer version 8 及以上版本,且其路径应具有以下值的 Windows 注册表,并且其键 TabProcGrowth 应设为 0:

Post that the internet explorer will be opened with clean session data and will not store the modified session data at termination. To achieve that, pass the capabilities - orceCreateProcessApi with the value true and browserCommandLineSwitches with the value -private. This is only applicable to internet explorer version 8 and above and windows registry having the below value and its path should have the key TabProcGrowth set to 0 −

HKLM_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\Main.

为了在 InternetExplorer 初始化时清理会话,我们需要传递功能 ensureCleanSession,其值为 true。这将帮助清除 Internet Explorer 的所有执行实例的缓存。

To clean the session at the InternetExplorer initiation, we need to pass the capability ensureCleanSession with the true value. It will help to clear the cache for all the executing instances of the internet explorer.

Execute IEDriverServer.exe Remotely

为了在远程计算机内执行 Internet Explorer 驱动程序,我们需要使用独立远程服务器,同时连接类似于 RemoteWebDriver 的语言绑定。

To execute the internet explorer driver inside a remote machine, we need to use the standalone remote server in connection with the language binding similar to the RemoteWebDriver.

Execute IEDriverServer.exe Under Windows Service

不支持在 Windows 服务应用程序内执行 Internet Explorer 驱动程序。而且,尚未对此进行测试。可能可以在 Windows 服务应用程序内运行 Internet Explorer 驱动程序,但可能存在需要解决的许多问题。

Executing the internet explorer driver within the Windows Service application is not supported. It is also not tested yet in this regard. It may be possible to run the internet explorer driver within the Windows Service application, but there may be many problems which need to be resolved.

Conclusion

这总结了我们对 Selenium IE 驱动程序教程的全面讲解。我们先从描述 IE 驱动程序的安装、IE 驱动程序的优点和缺点、什么是命令行开关、什么是至关重要的系统属性、IE 驱动程序配置的前提条件、什么是本机事件和 Internet Explorer、InternetExplorerDriver 的多个实例、远程执行 IEDriverServer.exe 以及在 Windows 服务下执行 IEDriverServer.exe 开始。

This concludes our comprehensive take on the tutorial on Selenium IE Driver. We’ve started with describing IE Driver Installation, advantages and disadvantages of IE Driver, what are the Command Line Switches, what are Vital System Properties, prerequisites for IE Driver Configurations, what are Native Events and Internet Explorer, multiple instances of InternetExplorerDriver, execute IEDriverServer.exe Remotely, and execute IEDriverServer.exe Under Windows Service.

这使你具备了有关 Selenium IE 驱动程序的深入知识。明智的做法是继续实践你所学到的内容,并探索与 Selenium 相关的其他内容,以便加深你的理解,拓宽你的视野。

This equips you with in-depth knowledge of the Selenium IE Driver. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.