selenium在使用python打开页面后无法获取该页面

2024-09-27 22:32:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码:

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.implicitly_wait(2)

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()

我尝试打开的任何页面都会在一段时间后返回超时错误:

Traceback (most recent call last):
  File "C:\Users\MunteanuG\AppData\Local\Programs\Python\Python38\lib\unittest\case.py", line 60, in testPartExecutor
    yield
  File "C:\Users\MunteanuG\AppData\Local\Programs\Python\Python38\lib\unittest\case.py", line 672, in run
    self._callSetUp()
  File "C:\Users\MunteanuG\AppData\Local\Programs\Python\Python38\lib\unittest\case.py", line 630, in _callSetUp
    self.setUp()
  File "C:\Users\MunteanuG\PycharmProjects\Dex_Automation\SRC\utilityTools.py", line 24, in setUp
    self.browser.get("https://www.google.ro/?safe=active&ssui=on")
  File "C:\Users\MunteanuG\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 333, in get
    self.execute(Command.GET, {'url': url})
  File "C:\Users\MunteanuG\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\MunteanuG\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: Timed out waiting for page to load.

我使用的是python 3.7解释器。 主要发布代码,向Post添加文本,测试


Tags: inpyimporttruelocalseleniumlineusers
2条回答

您试图加载的页面似乎正在加载某种JavaScript或无法访问

尝试使用以下命令禁用JavaScript

from selenium.webdriver.Ie.options import Options

options = Options()
options.preferences.update({'javascript.enabled': False})
browser = webdriver.Ie(options=options,executable_path="path")

如果仍然不起作用,请尝试删除所有功能

如果你研究一下Internet Explorer DriverRequired Configuration,你会清楚地看到以下几点:

保护模式

在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.


解决方案

您可以使用以下解决方案访问urlhttps://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')

参考资料

您可以在以下内容中找到一些相关的详细讨论:

相关问题 更多 >

    热门问题