i use python and pyqt4 for web view.
here my simple code :
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
import os
app = QApplication(sys.argv)
web_view= QWebView()
google='https://www.google.com'
web_view.load(QUrl(google))
web_view.show()
sys.exit(app.exec_())
i want to create a new button where that button any time to execute i want to take current url text link and store it in some variable.
how to do that ?how to take current url from web view ?
QWebView has the url() method that returns the current url, you must call it when you want to get the url.
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
app = QApplication(sys.argv)
w = QWidget()
lay = QVBoxLayout(w)
button = QPushButton("Click Me")
web_view= QWebView()
lay.addWidget(button)
lay.addWidget(web_view)
def foo():
print(web_view.url().toString())
button.clicked.connect(foo)
google='https://www.google.com'
web_view.load(QUrl(google))
w.show()
sys.exit(app.exec_())
Related
Is it possible to change the Save/Open/Cancel text of native file dialogs called by Qt, e.g.
from PyQt5.Qt import *
import sys
app = QApplication(sys.argv)
OpenFile = QFileDialog()
OpenFile.getExistingDirectory()
I've tried following some examples in C++, e.g. this, but it doesn't seem to have any effect. Maybe I'm doing something wrong?
Try it:
from PyQt5.Qt import *
import sys
app = QApplication(sys.argv)
OpenFile = QFileDialog()
#OpenFile.getExistingDirectory()
OpenFile.setFileMode(QFileDialog.DirectoryOnly)
OpenFile.setLabelText(QFileDialog.Accept, "+Accept+")
OpenFile.setLabelText(QFileDialog.Reject, "-REJECT-")
OpenFile.show()
sys.exit(app.exec_())
Here's my problem: I want to load a local html file into a QWebView in Python.
EDIT: I use PySide as a Qt package.
My code:
class myWindow(QWidget):
def __init__(self, parent=None):
self.view = QWebView(self)
filepath = "file://" + os.path.join(os.path.dirname(__file__), 'googlemap.html')
self.view.load(QUrl(filepath))
This is just showing me a blank widget.
If I change
self.view.load(QUrl(filepath)
by
self.view.load(QUrl("http://www.google.com/"))
It works fine.
However, the file is clearly in the good directory and I can open the same file directly with my browser.
EDIT 2:
The problem appears after an update on my Raspberry Pi 2 (which runs the code above)
Two observations:
path needs to be absolute (not relative)
use QUrl.fromLocalFile(path)
so something like this
file_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "aa.html"))
local_url = QUrl.fromLocalFile(file_path)
browser.load(local_url)
should work.
Full example:
from PyQt4.QtWebKit import QWebView
from PyQt4.QtGui import QApplication
from PyQt4.QtCore import QUrl
import sys
import os
app = QApplication(sys.argv)
browser = QWebView()
file_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "aa.html"))
local_url = QUrl.fromLocalFile(file_path)
browser.load(local_url)
browser.show()
app.exec_()
i'm trying to show a webpage in a frame, but i can't figure out how to do it, also because i can't find the right documentation and/or tutorial for the QtWebkit.
Thanks.
import sys
from PyQt4 import QtGui, QtCore, QtWebKit
class MainWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.resize(350, 250)
self.setWindowTitle('MainWindow')
self.statusBar().showMessage('Loading...')
self.web = QtWebKit.QWebView()
self.web.load(QtCore.QUrl('google.com'))
self.web.show()
app = QtGui.QApplication(sys.argv)
main = MainWindow()
main.show()
sys.exit(app.exec_())
for some doc, you can try the riverbank documentation (though code examples are still in C…)
It seems that your code is fine (maybe add http://?. But did you try to do that without classes? This should work:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
app = QApplication(sys.argv)
web = QWebView()
web.load(QUrl("http://google.com"))
web.show()
sys.exit(app.exec_())
Otherwise, there is a problem somewhere else (a proxy maybe?)
I would like to use PyQt/QWebview to 1) load a specific url, 2) enter information into a form, 3) click buttons/links. Mechanize does not work because I need an actual browser.
Here's my code:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
from PyQt4 import QtCore
app = QApplication(sys.argv)
web = QWebView()
web.load(QUrl("https://www.lendingclub.com/account/gotoLogin.action"))
def fillForm():
doc = web.page().mainFrame().documentElement()
user = doc.findFirst("input[id=master_username]")
passwd = doc.findFirst("input[id=master_password]")
user.setAttribute("value", "email#email.com")
passwd.setAttribute("value", "password")
button = doc.findFirst("input[id=master_sign-in-submit]")
button.evaluateJavaScript("click()")
QtCore.QObject.connect(web, QtCore.SIGNAL("loadFinished"), fillForm)
web.show()
sys.exit(app.exec_())
The page loads correctly, but no input is entered and the form is not submitted. Any ideas?
This helped me to make it work:
user.setAttribute("value", "email#email.com")
-->
user.evaluateJavaScript("this.value = 'email#email.com'")
Attribute and property are different things.
One more fix:
click() --> this.click()
For anyone looking to do this with PyQt5, this example may help as several things have changed. Obviously the javascript needs to be adjusted based on the contents of the website.
import os
import sys
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget
from PyQt5.QtCore import QUrl, QEventLoop
from PyQt5.QtWebEngineWidgets import QWebEngineView
class WebPage(QWebEngineView):
def __init__(self):
QWebEngineView.__init__(self)
self.load(QUrl("https://www.url.com"))
self.loadFinished.connect(self._on_load_finished)
def _on_load_finished(self):
print("Finished Loading")
self.page().toHtml(self.Callable)
def Callable(self, html_str):
self.html = html_str
self.page().runJavaScript("document.getElementsByName('loginid')[0].value = 'email#email.com'")
self.page().runJavaScript("document.getElementsByName('password')[0].value = 'test'")
self.page().runJavaScript ("document.getElementById('signin').click()")
if __name__ == "__main__":
app = QApplication(sys.argv)
web = WebPage()
web.show()
sys.exit(app.exec_()) # only need one app, one running event loop
You might be able to do it with Webkit/QWebView but what about using selenium: http://code.google.com/p/selenium/ ? It is designed for exactly this kind of browser automation and has nice python bindings.
I'm trying to take a screenshot of the curent window using a python script on linux.
I curently have a script which takes a screenshot of the entire screen:
import sys
from PyQt4.QtGui import QPixmap, QApplication
from datetime import datetime
date = datetime.now()
filename = date.strftime('%Y-%m-%d_%H-%M-%S.jpg')
app = QApplication(sys.argv)
QPixmap.grabWindow(QApplication.desktop().winId()).save(filename, 'jpg')
But a would like to have only the selected window. I know that the problem comes from grabWindow. But I don't know how to resolve it.
simply replace
QApplication.desktop()
with the widget you want to take the screenshot of.
import sys
from PyQt4.QtGui import *
from datetime import datetime
date = datetime.now()
filename = date.strftime('%Y-%m-%d_%H-%M-%S.jpg')
app = QApplication(sys.argv)
widget = QWidget()
# set up the QWidget...
widget.setLayout(QVBoxLayout())
label = QLabel()
widget.layout().addWidget(label)
def shoot():
p = QPixmap.grabWindow(widget.winId())
p.save(filename, 'jpg')
label.setPixmap(p) # just for fun :)
print "shot taken"
widget.layout().addWidget(QPushButton('take screenshot', clicked=shoot))
widget.show()
app.exec_()
Since Qt5, grabWindow and grabWidget are obsolete (see Obsolete Members for QPixmap)
Instead, you can use QWidget.grab()
p=widget.grab()
Alternatively, instead of
p = QPixmap.grabWindow(widget.winId())
you can also use
p = QPixmap.grabWidget(widget)
PyQt5 update
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtGui import QPixmap, QScreen
from datetime import datetime
date = datetime.now()
filename = date.strftime('%Y-%m-%d_%H-%M-%S.jpg')
app = QApplication(sys.argv)
QScreen.grabWindow(app.primaryScreen(),
QApplication.desktop().winId()).save(filename, 'png')