2

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_())
ABM
  • 1,628
  • 2
  • 26
  • 42
  • 1
    This works for me when using youtube.com – TheLazyScripter May 07 '16 at 19:14
  • @TheLazyScripter can you try with a higher number, say 20, and count to make sure that it scrolls 20 times? – ABM May 07 '16 at 19:47
  • Why would the scroll amount matter, as long as it always ended at the bottom of the page? – TheLazyScripter May 07 '16 at 19:53
  • I'm finding that I get multiple scrolls when I do execute the above code, but I don't get more scrolls if I increase the number from 9 to 20 or 40 – ABM May 09 '16 at 14:28
  • Obviously, scroll-to-bottom is called every time the page loads and currently is set to scroll to the bottom of the page on the first loop. If you want the number of loops to affect the number of scrolls then use a while true. Use a counter to copy the scroll number and on each loop decrement the counter and then scroll by y1/origonal_number_loops. – TheLazyScripter May 09 '16 at 14:52
  • Thanks, @TheLazyScripter. I want it to scroll to the bottom so the page will load new content with javascript. Then scroll to the bottom of the newly loaded content so more content will load. Then scroll to the bottom of the newly loaded content again, and repeat X number of times. – ABM May 09 '16 at 15:56
  • Then I would set a global/known value (possibly held within the class that handels your webview) called `max_scroll` and a `counter`. and then replace your `for` loop with `def scroll_to_bottom(result): if counter <= max_scroll: #Scroll code here counter += 1` At any point that `max_scroll == counter` you can disconnect the signal – TheLazyScripter May 09 '16 at 17:36

0 Answers0