I'm running Python 3.1 and you would call me an advanced novice :)
My question is simple: I'm trying to make a simple program which asks the users for a URL (or multiple URLs) and then goes to the website and takes a screenshot (of the whole page, not just what can be seen in the browser without scrolling all the way down).
It's simpler then it sounds, I want to use an existing platform on the web, similar to this:
import subprocess
MYFILENAME = "google_screen"
MYURL = "www.google.com"
subprocess.Popen(['wget', '-O', MYFILENAME+'.png', 'http://images.websnapr.com/?url='+MYURL+'&size=s&nocache=82']).wait()
Although this website does not work :(, I'm wondering is it possible to do it with this website and if so, how? If it is not possible, are there any alternatives?
There is a package called webkit2png that you can use for this, its located: here
More information on this blog post
Example from blog post(copied to SO for preservation, read the blog post to understand it if you have issues):
#!/usr/bin/env python
import sys
import signal
from PyQt4.QtCore import *
from PyQt 4.QtGui import *
from PyQt4.QtWebKit import QWebPage
def onLoadFinished(result):
if not result:
print "Request failed"
sys.exit(1)
# Set the size of the (virtual) browser window
webpage.setViewportSize(webpage.mainFrame().contentsSize())
# Paint this frame into an image
image = QImage(webpage.viewportSize(), QImage.Format_ARGB32)
painter = QPainter(image)
webpage.mainFrame().render(painter)
painter.end()
image.save("output2.png")
sys.exit(0)
app = QApplication(sys.argv)
signal.signal(signal.SIGINT, signal.SIG_DFL)
webpage = QWebPage()
webpage.connect(webpage, SIGNAL("loadFinished(bool)"), onLoadFinished)
webpage.mainFrame().load(QUrl("http://www.google.com"))
sys.exit(app.exec_())
Edit: Link to the pyqt4 download page
You can use Selenium to get a screenshot, but it'll only be what is viewed by the browser.
Related
I'm trying to use the spotify web player on a python browser windows.
I try it in two ways:
import webview
webview.create_window('spotify', 'https://open.spotify.com')
webview.start()
import sys
import time
from PyQt5.Qt import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
web = QWebEngineView()
web.load(QUrl("https://open.spotify.com/"))
web.showFullScreen()
sys.exit(app.exec_())
After I log in with my account I get:
Enable secure playback in your browser.
Is there way I can do this? In the spotify support page it tells me to enable protected content playback or play drm. I am not sure if it is possible.
By #metatoaster 's suggestion, I tried to set the QTWEBENGINE_CHROMIUM_FLAGS environment variable to --widevine-path="/home/<myusername>/.mozilla/firefox/237oaqbb.default-release/gmp-widevinecdm/4.10.2449.0/libwidevinecdm.so"
After I got the location from locate libwidevinecdm.so, and it worked!
I tried with the spotify's path for the library but that didn't work.
I'm starting to play with QWebView in PySide.
I can load HTML into the web view and view it, however I can't seem to load online pages or set urls. Here is the example I've got:
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtWebKit import *
app = QApplication(sys.argv)
web = QWebView()
web.setUrl(QUrl("https://www.google.com"))
web.show()
sys.exit(app.exec_())
All I get from this is a nice blank white window. Anyone know what's missing?
Maybe you are behind a proxy.
I've experienced the same behavior and setting a Proxy for the Application helped.
from PySide.QtNetwork import QNetworkProxy
proxy = QNetworkProxy()
proxy.setType(QNetworkProxy.Socks5Proxy);
proxy.setType(QNetworkProxy.HttpProxy);
proxy.setHostName("*********");
proxy.setPort(****);
QNetworkProxy.setApplicationProxy(proxy);
I would like to automate the upload of a file to a website using PyQt4's QWebView, but there's a part I can't figure out yet. To upload the file, the website has a button, which opens a dialog from which you are supposed to select the local file. So, these are my questions :) Is there a way to control that dialog once I click the button? Is there a better way to achieve this?
edit
The website is https://maps.google.com/ and I'm uploading a .kml file through My Places > Create Map > Import.
It is possible what you're looking for is QWebPage::chooseFile() (I suppose it depends also on how the website is handling that). Reimplement that and see if it is sufficient. Do whatever you want and return the chosen file path.
EDIT: Now that you provided the link I tested and seems to work.
Ok, to begin let me start with background information and references.
The module that I will be using is pywin32 download here, specifically the win32gui, API reference here.
Now before you can manipulate the dialog you have to "navigate" to the window handle, the following uses win32.FindWindow API Reference here, which looks like this, where the two inputs are the lpclassName in this case #32770 (stands for a dialog) reference here and the lpWindowName which in this case is File Upload,
HWND WINAPI FindWindow(
_In_opt_ LPCTSTR lpClassName,
_In_opt_ LPCTSTR lpWindowName
);
Code to locate file handle:
import win32gui
control = win32gui.FindWindow("#32770", "File Upload")
And it stores the handle, which in my case was 721470.
The next step is locate the handles of the GUI objects in the dialog, i will show an example of the Cancel button. To find the handle, I wil be using FindWindowEx API reference here,
import win32con
import win32api
ButtonHandle = win32gui.FindWindowEx(control, 0, "Button", "Cancel");
win32api.SendMessage(ButtonHandle, win32con.BM_CLICK, 0, 0)
Reference here for the BM_CLICK and here for the SendMessage.
Final code:
import win32gui
import win32api
import win32con
window = win32gui.GetForegroundWindow()
title = win32gui.GetWindowText(window)
control = win32gui.FindWindow("#32770", "File Upload")
ButtonHandle = win32gui.FindWindowEx(control, 0, "Button", "Cancel")
win32api.SendMessage(ButtonHandle, win32con.BM_CLICK, 0, 0)
Another way is to use the watsup.winGuiAuto module, here, example below:
from watsup.winGuiAuto import *
optDialog = findTopWindow(wantedText="File Upload")
CancelButton = findControl(optDialog,wantedClass="Button", wantedText="Cancel")
clickButton(SaveButton)
But i believe the easiest way is to use autoit here, i have used it before in pyqt, to shoot out commands.
Hope this helps!
Additional References (pywin32 versions):
win32gui here
win32api here
Here's a pure PyQt4 demo that more or less reproduces the default implementation:
import sys
from PyQt4 import QtCore, QtGui, QtWebKit
class WebPage(QtWebKit.QWebPage):
def chooseFile(self, frame=None, path=''):
return QtGui.QFileDialog.getOpenFileName(self.parent(), '', path)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
view = QtWebKit.QWebView()
view.setPage(WebPage(view))
view.load(QtCore.QUrl('https://maps.google.com/'))
view.show()
sys.exit(app.exec_())
i have a link in my QWebkit, which points to a pdf file.
But when the link is clicked, it can't display the pdf file.
is there a way to make it happen?
If you enable plugins through QWebSettings and have a PDF viewer installed that provides a browser plugin such as Acrobat then you should see the PDF rendered using the plugin inside your QWebView:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
app = QApplication(sys.argv)
web = QWebView()
web.settings().setAttribute(QWebSettings.PluginsEnabled, True)
web.show()
web.load(QUrl('file:///C:/test/test.pdf')) # Change path to actual file.
sys.exit(app.exec_())
This code isn't working for me on Windows with the latest version of Acrobat X (it just shows a progress bar but no PDF - proof that the plugin is loading, just not working) but I'm sure this is how I've done it before. Give it a try and let me know.
Webkit doesn't include a PDF viewer. You'll need to have some way of rendering it - whether you pass it off to a different viewer (Adobe PDF viewer or something else), render it in the control in some way you devise (you could even try rendering it in JavaScript for fun).
This is Gary Hudges code for PyQt5:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtWebKitWidgets import *
from PyQt5.QtWebKit import *
from PyQt5.QtCore import *
app = QApplication(sys.argv)
web = QWebView()
web.settings().setAttribute(QWebSettings.PluginsEnabled, True)
web.show()
web.load(QUrl('file:///C:/data/progetti_miei/python/test.pdf')) # Change path to actual file.
sys.exit(app.exec_())
I am writing an application for an embedded device that has no mouse input. The web browser is very very basic and does not have any buttons, address bar, file bar , etc. For now, it simply just loads a web page. This web page uses javascript to catch key press events for actions.
The problem I am having is that when the browser loads, the key presses are not caught. I have tracked this problem down to what I believe is a focus problem. When the browser loads, it does not have focus until a mouse click occurs on the application. Since I do not have a mouse, that initial click cannot happen.
How can I make sure that the browser application gets focused correctly such that when I launch it from a terminal or script it will gain immediate focus and the key events can happen accordingly?
My code is as follows:
#!/usr/bin/env python
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
app = QApplication(sys.argv)
web = QWebView()
web.showFullScreen()
web.load(QUrl("http://www.google.com"))
sys.exit(app.exec_())
The QWidget.setFocus() did not work, assuming I used it correctly. Any help is appreciated. Thanks
I tried your code on my laptop and the QWebView had focus already - once Google had loaded I could type and my text appeared in the text box.
If it is a focus problem then as QWebView inherits QWidget you can call its setFocus() method. Here is your code with an extra line calling the QWebView's setFocus method once a page has loaded:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
app = QApplication(sys.argv)
web = QWebView()
web.loadFinished.connect(web.setFocus)
web.showFullScreen()
web.load(QUrl("http://www.google.com/"))
sys.exit(app.exec_())
You can set focus on the main frame:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
if __name__ == '__main__':
app = QApplication(sys.argv)
web = QWebView()
frame = web.page().mainFrame()
frame.setFocus()
web.showFullScreen()
web.load(QUrl("http://www.google.com"))
sys.exit(app.exec_())