python - Selenium 确定浏览器被卡住

标签 python browser selenium

我正在使用 selenium 来做一些涉及加载大量页面的事情,可能包含大量图像和/或 Flash 广告,这显然对浏览器造成相当大的压力,因为我的 Firefox 浏览器卡住了。 Selenium 是否能够检测浏览器是否被卡住,就像在任务管理器中达到“无响应”的可怕状态,并且如果我单击选项卡则不会聚焦? 如果没有,有人知道处理这种情况的方法吗?

编辑:

我最终使用了以下内容:

  browser = webdriver.Firefox()
  try:
    browser.set_page_load_timeout(_DEFAULT_LOAD_TIME)
    browser.get('http://' + domain)
  except TimeoutException as te:
    print "Loading %s timed out. Killing browser." % domain
    print te
    browser.close()

尽管如果浏览器进入(无响应)状态,它不会关闭浏览器,但由于 _DEFAULT_LOAD_TIME 足够低,它通常会在浏览器进入该状态之前设法终止浏览器。

最佳答案

至少你可以为 Selenium 设置一个超时。在以下示例中,网络驱动程序将等待最多 10 秒,然后才会终止浏览器并使测试失败。这样您就可以最终执行所有测试。

from selenium import webdriver

ff = webdriver.Firefox()
ff.implicitly_wait(10) # seconds
ff.get("http://somedomain/url_that_delays_loading")
myDynamicElement = ff.find_element_by_id("myDynamicElement")

查看更多信息 Selenium headquarters

关于python - Selenium 确定浏览器被卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14528001/

相关文章:

c# - IE 11 - webdriver.Quit() 不起作用

internet-explorer - 当用户使用 Selenium 代理 BrowserMob 时无法在 IE 中打开 https 站点

java - 小程序为空,具有绝对布局

c# - 为什么我的 MVC Controller 在提供图像时速度很慢

python - 使用 Selenium 进行网页抓取

python - 无法在 Lubuntu 上安装任何东西

python - 为odoo中的特定组发送通知

python - 显示在列中的嵌套列表

python - 使用nltk库提取关键词

android - 如何在 android 中从浏览器启动 Activity ?