![](/img/trans.png)
[英]Selenium webdriver with python- how to reload page if loading takes too long?
[英]selenium webdriver takes too long to load a page
我使用PhantomJS作为我的webdriver。 有时加载网页需要很长时间,但我不知道为什么
import time
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36'
driver = webdriver.PhantomJS(service_args=['--load-images=no'], desired_capabilities=dcap)
t=time.time()
driver.get('http://www.tibetculture.net/2012zyzy/zx/201509/t20150915_3939844.html')
print 'Time consuming:', time.time() - t
加载页面花了大约86秒。 在浏览器中,网页可以在几秒钟内加载,我不知道为什么webdriver PhantomJS花了这么长时间。 它出什么问题了?
有一个“挂起”脚本不断运行。 我要做的是设置页面加载超时 ,通过发出window.stop()
处理TimeoutException
:
from selenium.common.exceptions import TimeoutException
t = time.time()
driver.set_page_load_timeout(10)
try:
driver.get('http://www.tibetculture.net/2012zyzy/zx/201509/t20150915_3939844.html')
except TimeoutException:
driver.execute_script("window.stop();")
print('Time consuming:', time.time() - t)
print(driver.find_element_by_id("NewsTitle").text)
打印新闻标题(证明您现在可以在页面上找到元素并进行操作):
Time consuming: 10.590633869171143
让藏医药走出雪域高原
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.