【问题标题】:selenium unable to get page after opening it with python硒用python打开后无法获取页面
【发布时间】:2020-07-25 10:06:36
【问题描述】:

我有这个代码:

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 解释器。 主要发代码,加文字发帖,测试测试测试测试

【问题讨论】:

  • 在 browser.get 之后添加显式或隐式等待,直到元素可以点击
  • 嗯,但这有帮助吗?因为没有页面被识别为已打开或已完全加载,我想这是一个潜在的问题,我认为等待不会解决问题,但我会尝试
  • 检查您的 IE 是否能够手动加载相同的页面

标签: python selenium selenium-webdriver selenium-iedriver


【解决方案1】:

您尝试加载的页面似乎正在加载某种 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")

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

【讨论】:

  • 我从 selenium.webdriver.Ie.options 导入选项中收到此错误AttributeError: 'Options' object has no attribute 'preferences',未找到“Ie”,而是将其替换为“ie”,这似乎没问题,但来自选项类偏好不存在
  • 另外,我尝试了 chrome 驱动程序,它运行良好,所以问题出在我的 Internet Explorer 上
  • 我希望我可以使用 chrome,我被 IE 卡住了,甚至没有边缘......我必须让它工作
  • 嗯,我知道你应该可以通过更改浏览器配置来默认禁用javascript来做到这一点
  • 从工具中禁用它,它的行为似乎相同,页面中的某些内容仍未加载,在我尝试加载的任何页面上
【解决方案2】:

如果您查看Internet Explorer DriverRequired Configuration,则清楚地提到了以下几点:

保护模式

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

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

虽然使用该功能并不能解决根本问题。如果跨越保护模式边界,可能会导致非常意外的行为,包括挂起、元素位置不工作和点击不传播。为了提醒人们注意这个潜在问题,该功能被赋予了听起来很吓人的名字,例如 Java 中的 INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS.NET 中的 IntroduceInstabilityByIgnoringProtectedModeSettings。我们真的认为告诉用户使用此设置会在他们的代码中引入潜在的错误会阻止其使用,但事实并非如此。


解决方案

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

参考文献

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多