3

I'm using selenium with python 3 and Chrome driver. I'm trying to perform a scroll in a chrome store URL.

This one: https://chrome.google.com/webstore/detail/online-game-zone-new-tab/abalcghoakdcaalbfadaacmapphamklh

No matter what I tried - the site doesn't scroll. Tried running "window.scrollBy" and "window.scrollTo". Tried clicking on "body" first, and other elements - nothing.

current code:

driver.maximize_window()
driver.get(url)
time.sleep(4)
driver.execute_script("window.scrollBy(0, 500)")

What could be the problem? I don't want to send a "down" key or "page down" since I believe I'll need to be more precise.

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
Oron Werner
  • 549
  • 4
  • 10

2 Answers2

1

Scrolling an element into view seems to be working fine on that webpage. Here's an example:

footer=driver.find_element_by_class_name('e-f-ra-gj')
driver.execute_script("arguments[0].scrollIntoView()", footer)
0buz
  • 3,121
  • 2
  • 6
  • 25
  • Thank you so much for that! But how can I use this to preform a scrollBy? What if I need to only scroll by a specific amount? Is that possible to do by "length" and not by specific element? – Oron Werner May 19 '20 at 14:57
  • It's simply a more reliable and portable approach than `scrollTo` in my opinion, given that you are stepping into JavaScript realm with Python's `driver.execute_script`. – 0buz May 19 '20 at 16:03
  • 2
    Thank you so much for your answer. I've accepted DebanjanB answer since it's more detailed and answered everything I wanted. I do agree with you that it might be better to use an element, but I might need sometimes just a specific scroll. Thanks again! – Oron Werner May 19 '20 at 16:25
0

If you observe the chrome store page the page have a header that is fixed to the top of the page. So when the window object is invoked, the focus jumps at the top of the page, leaving the document behind the fixed header.

You can find a detailed discussion in offsetting an html anchor to adjust for fixed header

Additionally, at times ChromeDriver is pretty fast and the scrollTo() action fires before Chrome's default scroll to html anchor event. In these cases, a possible solution would be to induce some delay as follows:

setTimeout(function() {window.scrollTo(0, y);},1)

You can find a detailed discussion in JavaScript issue with scrollTo() in Chrome

As an alternative, to perform a scroll within the chrome store URL you need to induce WebDriverWait for the visibility_of_element_located() of your choice and you can use the following Locator Strategy based solution:

  • Using scrollIntoView():

    driver.get('https://chrome.google.com/webstore/detail/online-game-zone-new-tab/abalcghoakdcaalbfadaacmapphamklh')
    driver.execute_script("arguments[0].scrollIntoView();", WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h2[text()='Related']"))))
    
  • Browser Snapshot:

Related

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • Thank you so much for that! But how can I use this to preform a scrollBy? What if I need to only scroll by a specific amount? Is that possible to do by "length" and not by specific element? – Oron Werner May 19 '20 at 14:47
  • And one more thing - why was waiting for the site to load usind sleep() is not enough? I waited far longer after the site was loaded, so what is the difference from the until() function? – Oron Werner May 19 '20 at 14:54
  • @OronW Checkout the updated answer and let me know your thoughts. – DebanjanB May 19 '20 at 15:43
  • 1
    Thank you very much for the detailed answer! I have a lot to read now (in a good way) and I hope what you wrote would make it all much more clear. I had no idea such problems can occur. This answer is very clear and answers everything for me so I've accepted it. Now I'll start with the "homework" I got from it... – Oron Werner May 19 '20 at 16:22