How can one use PyQt QWebKit to repeatedly scroll to the bottom of a web page that loads more content every time that it reaches the bottom?
The following code opens a browser window, navigates to a url, and scrolls to the bottom once to load new content. It only scrolls to the bottom once though. I want it to scroll to the bottom multiple times.
I can't figure out why the loop in "scroll_to_bottom" doesn't set the scroll position to the bottom of the page each time.
view = QWebView()
url = "http://www.example.com"
view.settings().setAttribute(QWebSettings.JavascriptEnabled, True)
view.settings().setAttribute(QWebSettings.DeveloperExtrasEnabled, True)
page = view.page()
view.load(QUrl(url))
def scroll_to_bottom(result):
# First time scrolling to bottom works
y1 = page.mainFrame().contentsSize().height()
page.mainFrame().setScrollPosition(QPoint(10, y1))
# These next attempts do not scroll to the bottom of the page
for i in range(9):
# y1 shows the correct contents size height
y1 = page.mainFrame().contentsSize().height()
# This command isn't setting the scroll position to y1
page.mainFrame().setScrollPosition(QPoint(10, y1))
view.page().loadFinished.connect(scroll_to_bottom)
view.show()
sys.exit(app.exec_())