我在截取 Google 翻译页面的屏幕截图时遇到问题。下面的脚本在没有谷歌翻译的情况下工作正常(截图):
class Render(QWebPage):
def __init__(self, url):
self.web_page = QWebPage()
self.finished = False
s = self.web_page.settings()
s.setAttribute(QWebSettings.JavascriptCanOpenWindows, False)
s.setAttribute(QWebSettings.PluginsEnabled, True)
self.web_page.mainFrame().setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff)
self.web_page.loadFinished.connect(self._loadFinished)
self.web_page.mainFrame().load(QUrl(url))
def _loadFinished(self, result):
frame = self.web_page.mainFrame()
size = frame.contentsSize()
size.setWidth(1000)
self.web_page.setViewportSize(size)
image = QImage(self.web_page.viewportSize(), QImage.Format_ARGB32)
painter = QPainter(image)
frame.render(painter)
painter.end()
self.filepath="screenshot_name.jpg"
image.save(self.filepath)
self.finished = True
def run(url):
app=QApplication.instance()
if not app:
app = QApplication(sys.argv)
r = Render(url)
while not r.finished:
app.processEvents()
time.sleep(0.01)
return r.filepath
news_url="http://arabic-media.com/arabicnews.htm"
news_url_google_translate = "http://translate.google.com/translate?hl=en&sl=auto&tl=en&u="+news_url
run(news_url_google_translate)
但是我需要对翻译后的页面进行截图。谷歌翻译页面创建 iframe。所以我用下面的方法对内框(name=c)进行了截图:
frame = self.web_page.mainFrame().childFrames()[0]
不幸的是,我的脚本仍在拍摄黑色屏幕截图。