如何等到网页满载python

时间:2015-08-11 20:46:10

标签: python html selenium webpage

我尝试使用一个唯一的class_name,它只在页面完全加载后出现,但是对于其他一些reaseon,它会在它出现在屏幕上之前出现

try:
    WebDriverWait(self.browser, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, 'selo-fechado'))
except:
    pass

除了time.sleep(4)之外,我还能做什么才能等到页面加载?

2 个答案:

答案 0 :(得分:0)

我通过刷新网站并开始查看项目以及最后可见的内容来解决问题

答案 1 :(得分:0)

以下是我发现的最佳和最可靠的解决方案:

import sys  
from PyQt4.QtGui import *  
from PyQt4.QtCore import *  
from PyQt4.QtWebKit import *  
from lxml import html 

#Take this class for granted.Just use result of rendering.
class Render(QWebPage):  
  def __init__(self, url):  
    self.app = QApplication(sys.argv)  
    QWebPage.__init__(self)  
    self.loadFinished.connect(self._loadFinished)  
    self.mainFrame().load(QUrl(url))  
    self.app.exec_()  

  def _loadFinished(self, result):  
    self.frame = self.mainFrame()  
    self.app.quit()  

url = 'http://pycoders.com/archive/'  
r = Render(url)  
result = r.frame.toHtml()
#This step is important.Converting QString to Ascii for lxml to process
archive_links = html.fromstring(str(result.toAscii()))
print archive_links

此处提供更多信息:https://impythonist.wordpress.com/2015/01/06/ultimate-guide-for-scraping-javascript-rendered-web-pages/