Selenium python scroll in element

Bill Agee’s blog

πŸ€” Reflections on test infrastructure, with a twist of user empathy.

Scrolling to an Element with the Python Bindings for Selenium WebDriver

When using Selenium WebDriver, you might encounter a situation where you need to scroll an element into view.

By running the commands in the following steps, you can interactively try out a solution using Google Chrome.

1. Set up a scratch environment and install the selenium package

My usual habit when starting a scratch project like this one is to set up a clean Python environment with virtualenv:

# In your shell: mkdir scrolling cd scrolling/ virtualenv env . env/bin/activate pip install selenium

2. Launch the Python interpreter, open a Chrome session with the selenium package, and navigate to Google News

$ python . >>> from selenium import webdriver >>> d = webdriver.Chrome() >>> d.get("http://news.google.com/") 

3. Identify the element that serves as the heading for the «Most popular» section of the page, then scroll to it by executing the JavaScript scrollIntoView() function

>>> element = d.find_element_by_xpath("//span[.='Most popular']") >>> element.text u'Most popular' >>> d.execute_script("return arguments[0].scrollIntoView();", element) 

Note that the Python WebDriver bindings also offer the location_once_scrolled_into_view property, which currently scrolls the element into view when retrieved.

However, that property is noted in the selenium module docs as subject to change without warning — and it also places the element at the bottom of the viewport (rather than the top), so I prefer using scrollIntoView().

4. Scroll the element a few px down toward the center of the viewport, if necessary

After the code above scrolls the element to the top of the window, you may find you need to scroll the document backwards to scoot the element slightly towards the center of the window — this can be necessary if the element is hidden under another element (for example, a toolbar that blocks clicks to the element you’re interested in).

Such scrolling is easy to do — this JS scrolls the document backwards by 150px, placing your element closer to the center of the viewport:

>>> d.execute_script("window.scrollBy(0, -150);") 

That’s all for now; I suspect I’ll continue to run into other types of element scrolling issues when using WebDriver, so this post may become the first in a series!

Π˜ΡΡ‚ΠΎΡ‡Π½ΠΈΠΊ

selenium-webdriver
Π‘ΠΊΡ€ΠΎΠ»Π»ΠΈΠ½Π³

Π Π°Π·Π»ΠΈΡ‡Π½Ρ‹Π΅ ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΠΈ с использованиСм java Ρ€Π°Π·Π½Ρ‹ΠΌΠΈ способами

НиТС Π΄Π°Ρ‚ΡŒ Ρ€Π΅ΡˆΠ΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ½ΠΎ Ρ‚Π°ΠΊΠΆΠ΅ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒ Π² Π΄Ρ€ΡƒΠ³ΠΈΡ… ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°Π΅ΠΌΡ‹Ρ… языках программирования с Π½Π΅ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΌΠΈ измСнСниями синтаксиса

  1. Π’Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ ΠŸΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΠ° Π²Π½ΠΈΠ· ΠΏΠΎ страницам / Ρ€Π°Π·Π΄Π΅Π»Ρƒ / Ρ€Π°Π·Π΄Π΅Π»Π΅Π½ΠΈΡŽ Π½Π° Π²Π΅Π±-страницС, Π² Ρ‚ΠΎ врСмя ΠΊΠ°ΠΊ Π΅ΡΡ‚ΡŒ настраиваСмая полоса ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΠΈ (Π½Π΅ ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΠ° Π±Ρ€Π°ΡƒΠ·Π΅Ρ€Π°). НаТмитС здСсь. Для дСмонстрации ΠΈ ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠΈ полосы ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΠΈ ΠΈΠΌΠ΅Π΅Ρ‚ свой нСзависимый элСмСнт.

Π’ ΠΏΡ€ΠΈΠ²Π΅Π΄Π΅Π½Π½ΠΎΠΌ Π½ΠΈΠΆΠ΅ ΠΊΠΎΠ΄Π΅ ΠΏΠ΅Ρ€Π΅Π΄Π°ΠΉΡ‚Π΅ элСмСнт полосы ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΠΈ ΠΈ ΡƒΠΊΠ°ΠΆΠΈΡ‚Π΅ Ρ‚ΠΎΡ‡ΠΊΠΈ ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΠΈ.

 public static boolean scroll_Page(WebElement webelement, int scrollPoints) < try < System.out.println("---------------- Started - scroll_Page ----------------"); driver = ExecutionSetup.getDriver(); dragger = new Actions(driver); // drag downwards int numberOfPixelsToDragTheScrollbarDown = 10; for (int i = 10; i < scrollPoints; i = i + numberOfPixelsToDragTheScrollbarDown) < dragger.moveToElement(webelement).clickAndHold().moveByOffset(0, numberOfPixelsToDragTheScrollbarDown).release(webelement).build().perform(); >Thread.sleep(500); System.out.println("---------------- Ending - scroll_Page ----------------"); return true; > catch (Exception e) < System.out.println("---------------- scroll is unsucessfully done in scroll_Page ----------------"); e.printStackTrace(); return false; >> 
  1. Π’Ρ‹ΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΠΈ Π²Π²Π΅Ρ€Ρ… страницы / Ρ€Π°Π·Π΄Π΅Π»Π° / дСлСния Π½Π° Π²Π΅Π±-страницС, ΠΏΠΎΠΊΠ° Π΅ΡΡ‚ΡŒ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΡΠΊΠ°Ρ полоса ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΠΈ (Π½Π΅ ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΠ° Π±Ρ€Π°ΡƒΠ·Π΅Ρ€Π°). НаТмитС здСсь. Для дСмонстрации ΠΈ ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΊΠΈ полосы ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΠΈ ΠΈΠΌΠ΅Π΅Ρ‚ свой нСзависимый элСмСнт.

Π’ ΠΏΡ€ΠΈΠ²Π΅Π΄Π΅Π½Π½ΠΎΠΌ Π½ΠΈΠΆΠ΅ ΠΊΠΎΠ΄Π΅ ΠΏΠ΅Ρ€Π΅Π΄Π°ΠΉΡ‚Π΅ элСмСнт полосы ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΠΈ ΠΈ ΡƒΠΊΠ°ΠΆΠΈΡ‚Π΅ Ρ‚ΠΎΡ‡ΠΊΠΈ ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΠΈ.

public static boolean scroll_Page_Up(WebElement webelement, int scrollPoints) < try < System.out.println("---------------- Started - scroll_Page_Up ----------------"); driver = ExecutionSetup.getDriver(); dragger = new Actions(driver); // drag upwards int numberOfPixelsToDragTheScrollbarUp = -10; for (int i = scrollPoints; i >10; i = i + numberOfPixelsToDragTheScrollbarUp) < dragger.moveToElement(webelement).clickAndHold().moveByOffset(0, numberOfPixelsToDragTheScrollbarUp).release(webelement).build().perform(); >System.out.println("---------------- Ending - scroll_Page_Up ----------------"); return true; > catch (Exception e) < System.out.println("---------------- scroll is unsucessfully done in scroll_Page_Up----------------"); e.printStackTrace(); return false; >> 
  1. Π§Ρ‚ΠΎΠ±Ρ‹ Π²Ρ‹ΠΏΠΎΠ»Π½ΠΈΡ‚ΡŒ ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΡƒ Π²Π½ΠΈΠ·, ΠΊΠΎΠ³Π΄Π° нСсколько ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΠΈ Π±Ρ€Π°ΡƒΠ·Π΅Ρ€Π° (ВстроСнный Π±Ρ€Π°ΡƒΠ·Π΅Ρ€), ΠΈ Π²Ρ‹ Ρ…ΠΎΡ‚ΠΈΡ‚Π΅ ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΈΡ‚ΡŒ Π²Π½ΠΈΠ· с ΠΏΠΎΠΌΠΎΡ‰ΡŒΡŽ клавиши Β«Π’Π½ΠΈΠ·Β» . НаТмитС здСсь для дСмонстрации

Π’ ΠΏΡ€ΠΈΠ²Π΅Π΄Π΅Π½Π½ΠΎΠΌ Π½ΠΈΠΆΠ΅ ΠΊΠΎΠ΄Π΅ ΠΏΠ΅Ρ€Π΅Π΄Π°ΠΉΡ‚Π΅ свой элСмСнт области ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΠΈ, Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€ ΠΈ Π½Π°ΠΆΠΌΠΈΡ‚Π΅ ΠΊΠ½ΠΎΠΏΠΊΡƒ Β«Π²Π½ΠΈΠ·Β».

 public static boolean pageDown_New(WebElement webeScrollArea, int iLoopCount) < try < System.out.println("---------------- Started - pageDown_New ----------------"); driver = ExecutionSetup.getDriver(); dragger = new Actions(driver); for (int i = 0; i System.out.println"---------------- Ending - pageDown_New ----------------"); return true; > catch (Exception e) < System.out.println("---------------- Not able to do page down ----------------"); return false; >> 
  1. Π§Ρ‚ΠΎΠ±Ρ‹ Π²Ρ‹ΠΏΠΎΠ»Π½ΠΈΡ‚ΡŒ ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΡƒ Π²Π²Π΅Ρ€Ρ…, ΠΊΠΎΠ³Π΄Π° нСсколько ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΠΈ Π±Ρ€Π°ΡƒΠ·Π΅Ρ€Π° (ВстроСнный Π±Ρ€Π°ΡƒΠ·Π΅Ρ€), ΠΈ Π²Ρ‹ Ρ…ΠΎΡ‚ΠΈΡ‚Π΅ ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΈΡ‚ΡŒ Π²Π²Π΅Ρ€Ρ… с ΠΏΠΎΠΌΠΎΡ‰ΡŒΡŽ клавиши UP UP . НаТмитС здСсь для дСмонстрации

Π’ Π½ΠΈΠΆΠ΅ΠΏΡ€ΠΈΠ²Π΅Π΄Π΅Π½Π½ΠΎΠΌ ΠΊΠΎΠ΄Π΅ ΠΏΠ΅Ρ€Π΅Π΄Π°ΠΉΡ‚Π΅ свой элСмСнт области ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΠΈ, Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€ ΠΈ Π½Π°ΠΆΠΌΠΈΡ‚Π΅ ΠΊΠ»Π°Π²ΠΈΡˆΡƒ Π²Π²Π΅Ρ€Ρ….

public static boolean pageUp_New(WebElement webeScrollArea, int iLoopCount) < try < System.out.println("---------------- Started - pageUp_New ----------------"); driver = ExecutionSetup.getDriver(); dragger = new Actions(driver); for (int i = 0; i System.out.println("---------------- Ending - pageUp_New ----------------"); return true; > catch (Exception e) < System.out.println("---------------- Not able to do page up ----------------"); return false; >> 
  1. Π§Ρ‚ΠΎΠ±Ρ‹ Π²Ρ‹ΠΏΠΎΠ»Π½ΠΈΡ‚ΡŒ ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΡƒ Π²Π½ΠΈΠ·, ΠΊΠΎΠ³Π΄Π° нСсколько ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΠΈ Π±Ρ€Π°ΡƒΠ·Π΅Ρ€Π° (ВстроСнный Π±Ρ€Π°ΡƒΠ·Π΅Ρ€), ΠΈ Π²Ρ‹ Ρ…ΠΎΡ‚ΠΈΡ‚Π΅ ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΈΡ‚ΡŒ Π²Π½ΠΈΠ· с ΠΏΠΎΠΌΠΎΡ‰ΡŒΡŽ клавиши «Волько стрСлка Π²Π½ΠΈΠ·Β» . НаТмитС здСсь для дСмонстрации

Π’ ΠΏΡ€ΠΈΠ²Π΅Π΄Π΅Π½Π½ΠΎΠΌ Π½ΠΈΠΆΠ΅ ΠΊΠΎΠ΄Π΅ ΠΏΠ΅Ρ€Π΅Π΄Π°ΠΉΡ‚Π΅ свой элСмСнт области ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΠΈ, Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€ ΠΈ Π½Π°ΠΆΠΌΠΈΡ‚Π΅ ΠΊΠ»Π°Π²ΠΈΡˆΡƒ Β«Π²Π½ΠΈΠ·Β».

public static boolean scrollDown_Keys(WebElement webeScrollArea, int iLoopCount) < try < System.out.println("---------------- Started - scrollDown_Keys ----------------"); driver = ExecutionSetup.getDriver(); dragger = new Actions(driver); for (int i = 0; i System.out.println("---------------- Ending - scrollDown_Keys ----------------"); return true; > catch (Exception e) < System.out.println("---------------- Not able to do scroll down with keys----------------"); return false; >> 
  1. Π§Ρ‚ΠΎΠ±Ρ‹ Π²Ρ‹ΠΏΠΎΠ»Π½ΠΈΡ‚ΡŒ ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΡƒ Π²Π²Π΅Ρ€Ρ…, ΠΊΠΎΠ³Π΄Π° нСсколько ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΠΈ Π±Ρ€Π°ΡƒΠ·Π΅Ρ€Π° (ВстроСнный Π±Ρ€Π°ΡƒΠ·Π΅Ρ€), ΠΈ Π²Ρ‹ Ρ…ΠΎΡ‚ΠΈΡ‚Π΅ ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‡ΠΈΠ²Π°Ρ‚ΡŒ Π²Π²Π΅Ρ€Ρ… с ΠΏΠΎΠΌΠΎΡ‰ΡŒΡŽ клавиши «Волько стрСлка Π²Π²Π΅Ρ€Ρ…Β» . НаТмитС здСсь для дСмонстрации

Π’ ΠΏΡ€ΠΈΠ²Π΅Π΄Π΅Π½Π½ΠΎΠΌ Π½ΠΈΠΆΠ΅ ΠΊΠΎΠ΄Π΅ ΠΏΠ΅Ρ€Π΅Π΄Π°ΠΉΡ‚Π΅ свой элСмСнт области ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΠΈ, Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€ ΠΈ Π²Π²Π΅Π΄ΠΈΡ‚Π΅ Ρ‚Ρ€Π΅Π±ΡƒΠ΅ΠΌΡ‹ΠΉ ΠΊΠ»ΡŽΡ‡.

public static boolean scrollUp_Keys(WebElement webeScrollArea, int iLoopCount) < try < System.out.println("---------------- Started - scrollUp_Keys ----------------"); driver = ExecutionSetup.getDriver(); dragger = new Actions(driver); for (int i = 0; i System.out.println("---------------- Ending - scrollUp_Keys ----------------"); return true; > catch (Exception e) < System.out.println("---------------- Not able to do scroll up with keys----------------"); return false; >> 
  1. Π§Ρ‚ΠΎΠ±Ρ‹ Π²Ρ‹ΠΏΠΎΠ»Π½ΠΈΡ‚ΡŒ ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΡƒ Π²Π²Π΅Ρ€Ρ… / Π²Π½ΠΈΠ· ΠΏΡ€ΠΈ ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΠ΅ Π±Ρ€Π°ΡƒΠ·Π΅Ρ€Π° (встроСнный Π±Ρ€Π°ΡƒΠ·Π΅Ρ€), ΠΈ Π²Ρ‹ Ρ…ΠΎΡ‚ΠΈΡ‚Π΅ ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‡ΠΈΠ²Π°Ρ‚ΡŒ Π²Π²Π΅Ρ€Ρ… / Π²Π½ΠΈΠ· Ρ‚ΠΎΠ»ΡŒΠΊΠΎ с фиксированной Ρ‚ΠΎΡ‡ΠΊΠΎΠΉ . НаТмитС здСсь для дСмонстрации

Π’ ΠΏΡ€ΠΈΠ²Π΅Π΄Π΅Π½Π½ΠΎΠΌ Π½ΠΈΠΆΠ΅ ΠΊΠΎΠ΄Π΅ ΠΏΠ΅Ρ€Π΅Π΄Π°ΠΉΡ‚Π΅ свою Ρ‚ΠΎΡ‡ΠΊΡƒ ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΠΈ. ΠŸΠΎΠ»ΠΎΠΆΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎΠ΅ ΠΎΠ·Π½Π°Ρ‡Π°Π΅Ρ‚, Ρ‡Ρ‚ΠΎ Π²Π½ΠΈΠ· ΠΈ ΠΎΡ‚Ρ€ΠΈΡ†Π°Ρ‚Π΅Π»ΡŒΠ½Ρ‹Π΅ срСдства ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‡ΠΈΠ²Π°ΡŽΡ‚ΡΡ Π²Π²Π΅Ρ€Ρ….

public static boolean scroll_without_WebE(int scrollPoint) < JavascriptExecutor jse; try < System.out.println("---------------- Started - scroll_without_WebE ----------------"); driver = ExecutionSetup.getDriver(); jse = (JavascriptExecutor) driver; jse.executeScript("window.scrollBy(0," + scrollPoint + ")", ""); System.out.println("---------------- Ending - scroll_without_WebE ----------------"); return true; >catch (Exception e) < System.out.println("---------------- scroll is unsucessful in scroll_without_WebE ----------------"); e.printStackTrace(); return false; >> 
  1. Π§Ρ‚ΠΎΠ±Ρ‹ Π²Ρ‹ΠΏΠΎΠ»Π½ΠΈΡ‚ΡŒ ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΡƒ Π²Π²Π΅Ρ€Ρ… / Π²Π½ΠΈΠ· ΠΏΡ€ΠΈ ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΠ΅ Π±Ρ€Π°ΡƒΠ·Π΅Ρ€Π° (встроСнный Π±Ρ€Π°ΡƒΠ·Π΅Ρ€), ΠΈ Π²Ρ‹ Ρ…ΠΎΡ‚ΠΈΡ‚Π΅ ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‡ΠΈΠ²Π°Ρ‚ΡŒ Π²Π²Π΅Ρ€Ρ… / Π²Π½ΠΈΠ·, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΡΠ΄Π΅Π»Π°Ρ‚ΡŒ элСмСнт Π² Π²ΠΈΠ΄ΠΈΠΌΠΎΠΉ области ΠΈΠ»ΠΈ динамичСском ΠΏΡ€ΠΎΠΊΡ€ΡƒΡ‚ΠΊΠ΅ . НаТмитС здСсь для дСмонстрации

Π’ ΠΏΡ€ΠΈΠ²Π΅Π΄Π΅Π½Π½ΠΎΠΌ Π½ΠΈΠΆΠ΅ ΠΊΠΎΠ΄Π΅ ΠΏΠ΅Ρ€Π΅Π΄Π°ΠΉΡ‚Π΅ свой элСмСнт.

public static boolean scroll_to_WebE(WebElement webe) < try < System.out.println("---------------- Started - scroll_to_WebE ----------------"); driver = ExecutionSetup.getDriver(); ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", webe); System.out.println("---------------- Ending - scroll_to_WebE ----------------"); return true; >catch (Exception e) < System.out.println("---------------- scroll is unsucessful in scroll_to_WebE ----------------"); e.printStackTrace(); return false; >> 

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΠ΅. ΠŸΠΎΠΆΠ°Π»ΡƒΠΉΡΡ‚Π°, ΠΏΡ€ΠΎΠ²Π΅Ρ€ΡŒΡ‚Π΅ свой случай ΠΈ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠΉΡ‚Π΅ ΠΌΠ΅Ρ‚ΠΎΠ΄Ρ‹. Если ΠΊΠ°ΠΊΠΎΠΉ-Π»ΠΈΠ±ΠΎ случай отсутствуСт, Π΄Π°ΠΉΡ‚Π΅ ΠΌΠ½Π΅ Π·Π½Π°Ρ‚ΡŒ.

Π˜ΡΡ‚ΠΎΡ‡Π½ΠΈΠΊ

Π§ΠΈΡ‚Π°ΠΉΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅:  Java charat unknown source

Python Selenium – How to Scroll Down a Page?

Be on the Right Side of Change

If we want to move down to a page or want to search for something on a page that is not in view, we use scrolling to reach there. Is it possible to scroll a page automatically with selenium? Seleniumβ€˜s main feature does not have an option for scrolling. But we can achieve it with some additional javascript features enabled in Python by using the driver object. In selenium, it is possible to scroll down the page in three different ways. Today we will try to know about all three possible options.

Set Up the Environment

Since I have a passion for traveling, I love to read travel blogs. Today we will try to scroll a website called β€œThe 50 most visited tourist attractions in the world”.

So, let us start the process. First, we need to import the WebDriver from selenium and then create a driver object from it. Next, we need to specify the path of the ChromeDriver as we will be using a chrome browser to scroll the page. The maximize_window() method is available to have a better view. Then we will try to connect to the website using the driver.get() method. We will be using implicit wait for 10 seconds. A cookie policy will appear at the bottom of the page when we will be connected. We need to find the WebElement of β€œOK, got it” button to accept it and then click it.

from selenium import webdriver driver = webdriver.Chrome(executable_path = r'G:/chromedriver_win32/chromedriver.exe') driver.maximize_window() driver.get('https://www.lovehomeswap.com/blog/latest-news/the-50-most-visited-tourist-attractions-in-the-world') driver.implicitly_wait(10) cookie = driver.find_element_by_link_text('OK, got it') cookie.click()

Scroll Down the Page by Pixel

It is possible to scroll a page with pixel number. There is a method called execute_script() which enables us to scroll a page. The command goes like this:

driver.execute_script("window.scrollBy(0,500)","")

Here we need to input two parameters in the scrollBy() method. 0 is the starting pixel position or default pixel and 500 is the pixel position we want to scroll to. By changing these values, it is possible to scroll down from one place to another place. The second parameter of the execute_script() method will remain empty. Let’s try to do it on the website.

driver.execute_script("window.scrollBy(0,3000)","")

As we have set the 2 nd parameter of the scrollBy() method from 500 to 3000 we can see the scroll bar at the right-hand side does not remain in its default position. It has scrolled down a bit where the 3000-pixel position lies. By changing the second parameter we can visit certain places on a page with the help of this method.

Π§ΠΈΡ‚Π°ΠΉΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅:  ΠΠ»ΡŒΡ‚Π΅Ρ€Π½Π°Ρ‚ΠΈΠ²Π½Ρ‹ΠΉ тСкст

Scroll Down the Page Till the Element Found

Now we want to search for a specific element in the webpage but we don’t know the exact pixel position for that point. How can we scroll down to that specific element? that is also possible with the following command.

driver.execute_script("arguments[0].scrollIntoView();",Element)

To work with this command, at first we need to identify the element that we want to view, and then we will store it to a variable. Again we will use execute_script() method and it will take two parameters as well. We will input β€œ arguments[0].scrollIntoView(); ” as the first parameter and the variable that contains the identified element as the second parameter. Hopefully, the scroll bar will automatically move to the place where the element is located.

Let’s try to find the element «Niagra Falls» from the web page. We want to set our scroller to view this element automatically. Following code will be good enough.

niagara_falls = driver.find_element_by_link_text('Niagara Falls') driver.execute_script("arguments[0].scrollIntoView();",niagara_falls)

Here, we tried to find the element by link text and created a variable niagara_falls with the located WebElement . In the next line, we executed the command with driver.execute_script method.

Niagara Falls appears at the top of the page. sometimes it may not be visible due to the β€œLog in” bar. Then you need to scroll up a little to get the view.

Scroll Down Till the End of the Page

It is also possible with the execute_script method to scroll down to the end of any page. the command will look like this:

driver.execute_script("window.scrollBy(0,document.body.scrollHeight)")

Here again, we will use javascript statement inside the execute_script() method. The scrollBy() method will take two parameters. The first one is 0 as the initial starting point and the second one should be javascript defined β€œ document.body.scrollHeight ” as it helps the scroller to reach the ending point of the page. If we run the code we will see the page like this.

Π§ΠΈΡ‚Π°ΠΉΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅:  Ajax jquery html success

We can see from the right-hand side that the scrollbar reached the ending point of the page.

So, that’s all about the methods we use nowadays in selenium python to automatically scroll down the browser. I hope you will find this link useful to learn more about Selenium.

Yassin Mahmud is a blockchain enthusiast and content creator in the blockchain space. Driven by his passion for distributed technology, he switched to the blockchain sector from the MERN stack development. With over half a decade of experience at present, he is trying to contribute to the blockchain community with his writing. When Yassin is not writing, he can be found walking along the beaches or touring spectacular travel destinations.

Be on the Right Side of Change πŸš€

  • The world is changing exponentially. Disruptive technologies such as AI, crypto, and automation eliminate entire industries. πŸ€–
  • Do you feel uncertain and afraid of being replaced by machines, leaving you without money, purpose, or value? Fear not! There a way to not merely survive but thrive in this new world!
  • Finxter is here to help you stay ahead of the curve, so you can keep winning as paradigms shift.

Learning Resources πŸ§‘β€πŸ’»

⭐ Boost your skills. Join our free email academy with daily emails teaching exponential with 1000+ tutorials on AI, data science, Python, freelancing, and Blockchain development!

Join the Finxter Academy and unlock access to premium courses πŸ‘‘ to certify your skills in exponential technologies and programming.

New Finxter Tutorials:

Finxter Categories:

Π˜ΡΡ‚ΠΎΡ‡Π½ΠΈΠΊ

ΠžΡ†Π΅Π½ΠΈΡ‚Π΅ ΡΡ‚Π°Ρ‚ΡŒΡŽ