4

I want to scroll down the page with selenium and python in an loop.

browser.execute_script("window.scrollTo(0, 40)")

^ this works so far.

but how can I reference a variable which will increase with every iteration?

e.g.

def scroll():
    global xx
    xx = 10
    while True:
         browser.execute_script("window.scrollTo(0, xx)")
         xx += 10`

I can see the problem.. the input (window.scrollTo(0, xx)) is a string.

but I dont know how to fix it. Do I need to change the executed script?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
trebz
  • 43
  • 3

1 Answers1

5

Simply use one of the string interpolation options, for example:

def scroll():
    xx = 10
    while True:
        browser.execute_script("window.scrollTo(0, {})".format(xx))
        xx += 10
DeepSpace
  • 78,697
  • 11
  • 109
  • 154