如何等待网页完全加载的Python方法

2024-04-16 07:33:24 发布

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

我试着取一个唯一的类名,它只在页面完全加载后出现,但出于其他原因,它在屏幕上出现之前就出现了

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

我还能做些什么来等待页面加载,除了时间。睡觉(4) 是吗?在


Tags: toselfbrowserby屏幕原因页面element
2条回答

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

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/

我解决了这个问题,刷新了网站,开始查看项目和什么是最后可见的

相关问题 更多 >