我一直尝试使用 Selenium 在 Internet Explorer 模式下打开 Microsoft Edge,每次都遇到以下错误:
“消息:启动 Internet Explorer 时出现意外错误。所有区域的保护模式设置都不相同。所有区域的启用保护模式必须设置为相同的值(启用或禁用)。”
我的代码如下:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
ie_options = webdriver.IeOptions()
ie_options.attach_to_edge_chrome = True
ie_options.edge_executable_path = r"C:\Users\PC1\Downloads\IEDriverServer_Win32_4.14.0\IEDriverServer.exe"
driver = webdriver.Ie(options=ie_options)
driver.get("http://www.bing.com")
elem = driver.find_element(By.ID, 'sb_form_q')
elem.send_keys('WebDriver' + Keys.RETURN)
driver.quit()
我尝试在 Internet Explorer 中手动更改设置(尽管似乎我无法将“受限站点”更改为除“高”(默认)之外的任何内容),并且我还在组策略编辑器中启用了“保护模式”。
有人有解决此错误的经验或可以以任何方式提供帮助吗?
我建议您可以在注册表中编辑保护模式设置。注册表项的路径可以参考这篇文章。例如,在这里我禁用了所有区域(区域 1, 2, 3 ,4)的保护模式(将值设置为 3)。您可以参考下图:
此外,请注意,您需要将 IEDriverServer 可执行文件放置在 PATH 中。使用IEDriver所需的配置请参考此文档。
您使用的代码也存在一些问题。
ie_options.edge_executable_path
应指向 Microsoft Edge 可执行文件的路径。请更改如下代码行:
ie_options.edge_executable_path = "C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe"