获取 QtWebKit、PyQt4 的 QWebView 中元素的坐标并自动滚动到其位置
get coordinates of an element in QWebView of QtWebKit, PyQt4 and autoscroll to its position
这是一个简单的代码,可以在 QWebView
中打开一个网站(例如,yahoo.com)。完成加载网站后,它会滚动到某个位置(例如,QPoint(100, 300)
)。我们需要等待网站完成加载,否则它不会自动滚动,因此 loadFinished
信号。
但问题是:如何找到元素的坐标(比如 'All Stories' 在 yahoo.com)并自动滚动到它的位置,就像我在下图中手动执行的那样? QWebFrame
中有findFirstElement
、findAllElements
之类的函数,但我不知道如何使用它们找到x、y坐标?
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
def loadFinished():
web.page().mainFrame().setScrollPosition(QPoint(100, 300))
app = QApplication(sys.argv)
web = QWebView()
web.load(QUrl("http://yahoo.com"))
web.connect(web, SIGNAL('loadFinished(bool)'), loadFinished)
web.setGeometry(700, 500, 300, 500)
web.setWindowTitle('yahoo')
web.show()
sys.exit(app.exec_())
def loadFinished():
elements = web.page().mainFrame().findAllElements(css_selector)
for index in range(elements.count()):
print(elements.at(index).geometry())
对于任何感兴趣的人,这是我的最终代码(基于 ekhumoro 使用 QWebElement.geometry()
的建议):
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
def loadFinished():
html = web.page().mainFrame().documentElement()
# I couldn't find a way to find html element by text 'All Stories',
# so copied this weird unique attribute from yahoo page source code
el = html.findFirst('a[data-ylk="sec:strm;cpos:1;elm:itm;elmt:fltr;pos:0;ft:1;itc:1;t1:a3;t2:strm;t3:lst"]')
qp = el.geometry().topLeft() # returns QPoint object
# we can either use QPoint position as is:
# web.page().mainFrame().setScrollPosition(qp)
# or calibrate x, y coordinates a little:
web.page().mainFrame().setScrollPosition(QPoint(qp.x() - 15, qp.y()))
app = QApplication(sys.argv)
web = QWebView()
web.load(QUrl("http://yahoo.com"))
web.loadFinished.connect(loadFinished)
web.setGeometry(700, 500, 300, 500)
web.setWindowTitle('yahoo')
web.show()
sys.exit(app.exec_())
这是一个简单的代码,可以在 QWebView
中打开一个网站(例如,yahoo.com)。完成加载网站后,它会滚动到某个位置(例如,QPoint(100, 300)
)。我们需要等待网站完成加载,否则它不会自动滚动,因此 loadFinished
信号。
但问题是:如何找到元素的坐标(比如 'All Stories' 在 yahoo.com)并自动滚动到它的位置,就像我在下图中手动执行的那样? QWebFrame
中有findFirstElement
、findAllElements
之类的函数,但我不知道如何使用它们找到x、y坐标?
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
def loadFinished():
web.page().mainFrame().setScrollPosition(QPoint(100, 300))
app = QApplication(sys.argv)
web = QWebView()
web.load(QUrl("http://yahoo.com"))
web.connect(web, SIGNAL('loadFinished(bool)'), loadFinished)
web.setGeometry(700, 500, 300, 500)
web.setWindowTitle('yahoo')
web.show()
sys.exit(app.exec_())
def loadFinished():
elements = web.page().mainFrame().findAllElements(css_selector)
for index in range(elements.count()):
print(elements.at(index).geometry())
对于任何感兴趣的人,这是我的最终代码(基于 ekhumoro 使用 QWebElement.geometry()
的建议):
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
def loadFinished():
html = web.page().mainFrame().documentElement()
# I couldn't find a way to find html element by text 'All Stories',
# so copied this weird unique attribute from yahoo page source code
el = html.findFirst('a[data-ylk="sec:strm;cpos:1;elm:itm;elmt:fltr;pos:0;ft:1;itc:1;t1:a3;t2:strm;t3:lst"]')
qp = el.geometry().topLeft() # returns QPoint object
# we can either use QPoint position as is:
# web.page().mainFrame().setScrollPosition(qp)
# or calibrate x, y coordinates a little:
web.page().mainFrame().setScrollPosition(QPoint(qp.x() - 15, qp.y()))
app = QApplication(sys.argv)
web = QWebView()
web.load(QUrl("http://yahoo.com"))
web.loadFinished.connect(loadFinished)
web.setGeometry(700, 500, 300, 500)
web.setWindowTitle('yahoo')
web.show()
sys.exit(app.exec_())