Forum Archive

How to use script rolling the page down when using WebView

louxingliang

Hi, I've read the document of WebView on this website, but still I haven't find the method to do this.
If I open a website which is very long, how to use script to roll down the page ..?

cvp

@louxingliang very quick and dirty code

import ui
from objc_util import *
w = ui.WebView()
wo = ObjCInstance(w)
sv = wo.subviews()[0].scrollView()
w.load_url('http://omz-software.com/pythonista/')
w.frame = (00,0,300,300)
bu = ui.ButtonItem()
bu.title = 'up'
w.top = 0
def scroll_up(sender):
    w.top = w.top - 300
    if w.top < 0:
        w_top = 0
    sv.setContentOffset_(CGPoint(0,w.top))
bu.action = scroll_up
bd = ui.ButtonItem()
bd.title = 'down'
def scroll_down(sender):
    w.top = w.top + 300
    sv.setContentOffset_(CGPoint(0,w.top))
bd.action = scroll_down
w.right_button_items = (bd,bu)
w.present('sheet')

Edit: little bug, was w_to = 0, sorry

louxingliang

Thanks again! Ready to try it.
You are really familiar with the python lib !

cvp

@louxingliang Not at all, but a very quick search with Google "Pythonista webview scroll" gives 😂

cvp

@louxingliang and now without Objective-C by setting the webview as sub view of a scrollview

import ui
sv = ui.ScrollView()
w = ui.WebView()
w.load_url('http://omz-software.com/pythonista/')
sv.frame = (00,0,300,300)
w.frame = sv.frame
sv.add_subview(w)
bu = ui.ButtonItem()
bu.title = 'up'
w.top = 0
def scroll_up(sender):
    w.top = w.top - sv.height
    if w.top < 0:
        w_top = 0
    w_height = w.height - sv.height
    if w_height >= sv.height:
        w.height = w_height
        sv.content_size = (sv.width,w.height)
        sv.content_offset =  (0,w.top)
bu.action = scroll_up
bd = ui.ButtonItem()
bd.title = 'down'
def scroll_down(sender):
    w.top = w.top + sv.height
    w.height = w.height + sv.height
    sv.content_size = (sv.width,w.height)
    sv.content_offset =  (0,w.top)
bd.action = scroll_down
sv.right_button_items = (bd,bu)
sv.present('sheet')
louxingliang

@cvp recently I met a new problem.
I can not open the website with user-agent request head when using load_url.
I tried this way, but it doesn’t work
headers={
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.4882.400 QQBrowser/9.7.13059.400'}
w.loadRequest(url,request)

Is there any way to solve this problem?

cvp

@louxingliang Oups, I Really can't help you, I'm not a specialist of web browser.
But I'm sure that this marvelous forum will provide somebody who will help you successfully

mikael

@louxingliang, WKWebKit has a user_agent property that you can set before loading, see here.

cvp

I forgot that. Thanks for helping

mikael

For completeness sake, one more way to scroll would be a Javascript one-liner, let me know if that would relevant for you.