问题描述
我正在Windows7上使用硒和python.
I'm using selenium and python on windows7.
我的代码:
import os
from selenium import webdriver
# get the path of IEDriverServer
#dir = os.path.dirname(__file__)
#ie_driver_path = dir + "\IEDriverServer.exe"
ie_driver_path = "C:\Python36\Scripts\IEDriverServer.exe"
# create a new Internet Explorer session
driver = webdriver.Ie(ie_driver_path)
driver.implicitly_wait(30)
driver.maximize_window()
# create a new Firefox session
#driver = webdriver.Firefox()
#driver.implicitly_wait(30)
#driver.maximize_window()
# navigate to the application home page
driver.get("http://demo-store.seleniumacademy.com/")
# get the search textbox
search_field = driver.find_element_by_name("q")
search_field.clear()
# enter search keyword and submit
search_field.send_keys("phones")
search_field.submit()
...
该代码有效,但是当打开时,即电话"的数字化非常慢(大约20秒).相反,在firefox中几乎是瞬时的.
The code works but when opens Ie the digitation of "phones" is very slow (about 20 seconds). In firefox instead is almost instantaneous.
为什么呢?这是正常的?我做错了什么?
Why so? It is normal? I'm doing something wrong?
PS:另外,将IEDriverServer.exe放在哪里更好?在C:\Python36\Scripts
内部,所以我所有项目或每个项目内部都只有一个文件(例如注释掉的部分)?
PS: also, where is it better to put my IEDriverServer.exe? Inside C:\Python36\Scripts
so I have just one file for all my projects or inside each projects (like in the part commented out)?
推荐答案
是的,您没看错.
使用 64位IEDriverServer.exe send_keys()
会非常缓慢地用字符序列填充字段.
Using 64-bit IEDriverServer.exe send_keys()
populates the field with the character sequence very slowly.
@JimEvans在文章屏幕截图,SendKeys,和"64位元" 提到 ...自IE10发布以来,Internet Explorer驱动程序存在一些问题... .
@JimEvans in the article Screenshots, SendKeys, and Sixty-Four Bits mentions that ...there are a couple of issues with the Internet Explorer driver that have been around since IE10 was released....
讨论中的评论 IE x64慢速键入提到,任何修补程序都需要对IE驱动程序的二进制组件进行大规模重新架构,[因此]没有(或将要提供)时间表"来提供修补程序.是什么原因导致这些问题?它们有什么关系?为什么修复是如此困难?这些问题的答案都可以用一个简单的答案来概括:"Windows挂钩".
Comments in the discussion IE x64 slow typing mentions that any fix would require "a massive rearchitecture of the IE driver's binary components, [so] no timeline is (or will be) available" for the delivery of a fix. What causes these issues? How are they related? Why would a fix be so darned difficult? The answers to those questions can all be summed up with a simple answer: "Windows Hooks."
在64位版本的Windows上运行IE 10或更高版本时,默认情况下,托管包含浏览器镶边(包含地址栏,导航按钮,菜单等)的包含窗口的进程为64-位过程.托管实际呈现内容的窗口(在每个选项卡中)的进程是一个32位进程.
When you are running IE 10 or higher on a 64-bit version of Windows, by default the process which hosts the containing window that includes the browser chrome (address bar, navigation buttons, menus, etc.) is a 64-bit process. The process which hosts the window where content is actually rendered (within each tab) is a 32-bit process.
默认情况下,IE驱动程序尝试在内容呈现窗口上使用Windows钩子,以确保在发送按键消息之前,正确处理了按键消息.这就是问题所在.未安装Windows挂钩,因为32位进程(内容呈现进程)无法执行64位代码.正确解决此问题的唯一方法是创建第二个(32位)可执行文件,以执行等待击键完成的等待.由于这将构成IE驱动程序二进制组件的大量重新架构,因此没有(或将提供)此更改的时间表.这意味着,即使在运行64位Windows时,也可能会使用32位版本的IE来呈现内容.对于继续将IE驱动程序的32位版本用于IE 10及更高版本,这是一个有力的论据:实际上,您不是实际上针对64位版本的IE运行.
By default, the IE driver attempts to use a windows hook on the content-rendering window to make sure that a key-down message is properly processed before sending a key-up message. This is where the problem is. The windows hook is not installed, because a 32-bit process (the content-rendering process) can't execute 64-bit code. The only way to properly fix this will be to create a second (32-bit) executable to perform the wait for the key-down to be complete. Since this would amount to a massive rearchitecture of the IE driver's binary components, no timeline is (or will be) available for this change. This means that even when you are running 64-bit Windows, you're likely using a 32-bit version of IE to render the content. This is a powerful argument for continuing to use the 32-bit version of the IE driver for IE 10 and above: you're not actually running against a 64-bit version of IE.
如果坚持必须运行IEDriverServer.exe的64位版本,则有两种可能的解决方法.首先,可以使用语言绑定为此提供的任何机制,通过将"nativeEvents"功能设置为false来禁用本机事件.从输入模拟的角度来看,更准确的解决方法是启用"requireWindowFocus"功能,尽管它也具有Windows钩子依赖关系,这可能会以其他方式体现出来.
If you insist that you must run the 64-bit version of IEDriverServer.exe, you have two possible workarounds. First, you can disable native events by setting the "nativeEvents" capability to false using whatever mechanism your language binding provides for this. A more accurate workaround from an input simulation perspective would be to enable the "requireWindowFocus" capability, though this also has a windows hook dependency, which may manifest in other ways.
所有Windows应用程序中都有一个称为消息循环"的例程.消息循环反复调用GetMessage API函数,并在发送到应用程序的消息到达其队列时处理消息.挂钩是Windows消息处理系统的一项功能,允许开发人员拦截,检查和修改发送到应用程序的消息.通过安装钩子,开发人员可以例如验证某个消息是否已被钩住的窗口处理.或者他们可以修改发送到窗口的消息,以表示操作系统可以执行实际上无法执行的操作.这是一个聪明的机制,但确实有一些要求,超出了本文的讨论范围.
All Windows applications have a routine in them called a "message loop." The message loop repeatedly calls the GetMessage API function, and processes messages sent to the application as they arrive in its queue. Hooks are a feature of the Windows message handling system that allow a developer to intercept, examine, and modify the message being sent to the application. By installing a hook, a developer could, for example, validate that a certain message was processed by the window being hooked. Or they could modify a message sent to the window to represent that the operating system could do things it actually can't. It's a clever mechanism, but it does have a few requirements which is out of scope for this discussion.
代替 64位IEDriverServer 可执行文件,请尝试使用 32位IEDriverServer 可执行文件
Instead of 64-bit IEDriverServer executable try to use 32-bit IEDriverServer executable
您可以将 IEDriverServer.exe 放置在系统中的任何位置,并通过参数 executable_path
传递二进制文件的绝对位置,如下所示(以Windows OS为例):
You can put the IEDriverServer.exe anywhere within your system and pass the absolute location of the binary through the argument executable_path
as follows (Windows OS example):
from selenium import webdriver
driver = webdriver.Ie(executable_path=r'C:\path\to\IEDriverServer.exe')
driver.get("https://www.facebook.com/")
print("Page Title is : %s" %driver.title)
driver.quit()
这篇关于IEDriverServer使用Selenium将文本发送到搜索字段的速度非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!