python - selenium.common.exceptions.TimeoutException : Message: Timed out waiting for page to load using IEDriverServer and Internet Explorer through Selenium

标签 python selenium selenium-webdriver internet-explorer selenium-iedriver

鉴于此代码:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
# import org.openqa.selenium.Keys
import datetime
import time
import unittest

cap = DesiredCapabilities().INTERNETEXPLORER
cap['ignoreProtectedModeSettings'] = True
cap['IntroduceInstabilityByIgnoringProtectedModeSettings'] = True
cap['nativeEvents'] = True
cap['ignoreZoomSetting'] = True
cap['requireWindowFocus'] = True
cap['INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS'] = True
browser = webdriver.Ie(capabilities=cap, executable_path=r'C:\IEDriverServer_x64_3.150.1\IEDriverServer.exe')
browser.get("https://www.google.ro/?safe=active&ssui=on")
search_form = browser.find_element_by_xpath('/html[1]/body[1]/div[1]/div[1]/div[3]/div[1]/button[1]')
search_form.click()

当我运行它时,每次尝试,在任何网站上都会返回此错误:

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: Timed out waiting for page to load.

在不同的 PC 上运行相同的代码工作正常,在我的情况下,我希望测试因找不到元素而失败,因为指定的 xpath 是错误的。

进程“卡在”

browser.get("https://www.google.ro/?safe=active&ssui=on")

好像页面没有正确加载。

我使用的是python 3.8.0 IEDriverServer_x64_3.150.1 Selenium 驱动程序

最佳答案

此错误消息...

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: Timed out waiting for page to load.

...暗示 IEDriverServer 可执行二进制文件无法启动/生成新的浏览上下文,即 Internet Explorer 浏览 session 。


根据 Required ConfigurationInternet Explorer Driver :

保护模式

在 Windows Vista 或 Windows 7 上的 Internet Explorer 7 或更高版本上,您必须将每个区域的保护模式设置设置为相同的值。该值可以打开或关闭,只要每个区域都相同即可。要设置保护模式设置,您必须从“工具”菜单中选择“Internet 选项”,然后单击安全选项卡。对于每个区域,选项卡底部都会有一个标记为启用保护模式的复选框。

ProtectedModeSettings

此外,@JimEvans 在他的文章 You're Doing It Wrong: IE Protected Mode and WebDriver 中明确提到:

Using the capability doesn't solve the underlying problem though. If a Protected Mode boundary is crossed, very unexpected behavior including hangs, element location not working, and clicks not being propagated, could result. To help warn people of this potential problem, the capability was given big scary-sounding names like INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS in Java and IntroduceInstabilityByIgnoringProtectedModeSettings in .NET. We really thought that telling the user that using this setting would introduce potential badness in their code would discourage its use, but it turned out not to be so.


解决方案

要使用 Selenium 访问网址 https://www.google.ro/?safe=active&ssui=on驱动发起 浏览上下文您可以使用以下最小代码块:

from selenium import webdriver

driver = webdriver.Ie(executable_path=r'C:\WebDrivers\IEDriverServer.exe')
driver.get('https://www.google.ro/?safe=active&ssui=on')

其他注意事项

确保执行以下步骤:

Note: As per best practices as Selenium Client and InternetExplorerDriver are released in sync and you must try to use both the binaries from the same major release.

  • 通过IDE清理您的项目工作区,并仅使用所需的依赖项重建项目。
  • 执行您的@Test
  • 始终在 tearDown(){} 方法中调用 driver.quit() 来关闭和销毁 WebDriverWeb Client 优雅地实例化。

引用文献

您可以在以下位置找到一些相关的详细讨论:

关于python - selenium.common.exceptions.TimeoutException : Message: Timed out waiting for page to load using IEDriverServer and Internet Explorer through Selenium,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63086747/

相关文章:

c# - 如何点击没有 ID 或名称的链接?

python - Selenium 与器件尺寸

java - 如何使用 Selenium 读取pdf文件

python - python中pandas中DataFrame的dropna中的thresh

python - 如何将特征的不确定性纳入机器学习算法?

python - 斐波那契内存: Cannot understand the reason for TypeError

java - 如何使用 Selenium 获取元素中文本的颜色?

Python输入空字符串后停止输入

java - 如何使用java处理phantomjs selenium中的javascript错误?

java - 如何使用appium+Java在Android应用程序中滚动?