问题
Given this code :
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()
When I run it, for every try, on any website this error is returned :
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: Timed out waiting for page to load.
Running the same code on a different PC works fine, in my case I want the test to fail with element not found, given the xpath specified is wrong.
The process is "stuck" at
browser.get("https://www.google.ro/?safe=active&ssui=on")
as if the page didn't load properly.
I am using python 3.8.0 IEDriverServer_x64_3.150.1 selenium driver
回答1:
This error message...
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: Timed out waiting for page to load.
...implies that the IEDriverServer executable binary was unable to initiate/spawn a new Browsing Context i.e. Internet Explorer Browsing session.
As per the Required Configuration of Internet Explorer Driver:
Protected Mode
On Internet Explorer 7 or higher on Windows Vista or Windows 7, you must set the Protected Mode settings for each zone to be the same value. The value can be on or off, as long as it is the same for every zone. To set the Protected Mode settings you have to choose "Internet Options" from the "Tools" menu and then click on the Security tab. For each zone, there will be a check box at the bottom of the tab labeled Enable Protected Mode.
Additionally, @JimEvans
in his article You're Doing It Wrong: IE Protected Mode and WebDriver clearly mentions :
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 andIntroduceInstabilityByIgnoringProtectedModeSettings
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.
Solution
To access the url https://www.google.ro/?safe=active&ssui=on
using Selenium driven selenium-iedriver initiated internet-explorer Browsing Context you can use the following minimal code block:
from selenium import webdriver
driver = webdriver.Ie(executable_path=r'C:\WebDrivers\IEDriverServer.exe')
driver.get('https://www.google.ro/?safe=active&ssui=on')
Additional Considerations
Ensure the following steps:
- Upgrade JDK to current levels JDK 8u251.
- Upgrade Selenium to current levels Version 3.141.59.
- Upgrade IEDriverServer to latest IEDriverServer v3.150.1 level.
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.
- Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
- Execute your
@Test
. - Always invoke
driver.quit()
withintearDown(){}
method to close & destroy the WebDriver and Web Client instances gracefully.
References
You can find a couple of relevant detailed discussions in:
- Internet Explorer Protective mode setting and Zoom levels
- How to ignore protected Mode Settings for Internet Explorer using setCapability() through Selenium and Java?
- How to ignore zoom setting
来源:https://stackoverflow.com/questions/63086747/selenium-common-exceptions-timeoutexception-message-timed-out-waiting-for-page