- 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
- 1. Set up a scratch environment and install the selenium package
- 2. Launch the Python interpreter, open a Chrome session with the selenium package, and navigate to Google News
- 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
- 4. Scroll the element a few px down toward the center of the viewport, if necessary
- selenium-webdriver Π‘ΠΊΡΠΎΠ»Π»ΠΈΠ½Π³
- Π Π°Π·Π»ΠΈΡΠ½ΡΠ΅ ΠΏΡΠΎΠΊΡΡΡΠΊΠΈ Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΠ΅ΠΌ java ΡΠ°Π·Π½ΡΠΌΠΈ ΡΠΏΠΎΡΠΎΠ±Π°ΠΌΠΈ
- Python Selenium β How to Scroll Down a Page?
- Set Up the Environment
- Scroll Down the Page by Pixel
- Scroll Down the Page Till the Element Found
- Scroll Down Till the End of the Page
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 ΡΠ°Π·Π½ΡΠΌΠΈ ΡΠΏΠΎΡΠΎΠ±Π°ΠΌΠΈ
ΠΠΈΠΆΠ΅ Π΄Π°ΡΡ ΡΠ΅ΡΠ΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ½ΠΎ ΡΠ°ΠΊΠΆΠ΅ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ Π² Π΄ΡΡΠ³ΠΈΡ ΠΏΠΎΠ΄Π΄Π΅ΡΠΆΠΈΠ²Π°Π΅ΠΌΡΡ ΡΠ·ΡΠΊΠ°Ρ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΠΈΡΠΎΠ²Π°Π½ΠΈΡ Ρ Π½Π΅ΠΊΠΎΡΠΎΡΡΠΌΠΈ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΡΠΌΠΈ ΡΠΈΠ½ΡΠ°ΠΊΡΠΈΡΠ°
- ΠΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ ΠΡΠΎΠΊΡΡΡΠΊΠ° Π²Π½ΠΈΠ· ΠΏΠΎ ΡΡΡΠ°Π½ΠΈΡΠ°ΠΌ / ΡΠ°Π·Π΄Π΅Π»Ρ / ΡΠ°Π·Π΄Π΅Π»Π΅Π½ΠΈΡ Π½Π° Π²Π΅Π±-ΡΡΡΠ°Π½ΠΈΡΠ΅, Π² ΡΠΎ Π²ΡΠ΅ΠΌΡ ΠΊΠ°ΠΊ Π΅ΡΡΡ Π½Π°ΡΡΡΠ°ΠΈΠ²Π°Π΅ΠΌΠ°Ρ ΠΏΠΎΠ»ΠΎΡΠ° ΠΏΡΠΎΠΊΡΡΡΠΊΠΈ (Π½Π΅ ΠΏΡΠΎΠΊΡΡΡΠΊΠ° Π±ΡΠ°ΡΠ·Π΅ΡΠ°). ΠΠ°ΠΆΠΌΠΈΡΠ΅ Π·Π΄Π΅ΡΡ. ΠΠ»Ρ Π΄Π΅ΠΌΠΎΠ½ΡΡΡΠ°ΡΠΈΠΈ ΠΈ ΠΏΡΠΎΠ²Π΅ΡΠΊΠΈ ΠΏΠΎΠ»ΠΎΡΡ ΠΏΡΠΎΠΊΡΡΡΠΊΠΈ ΠΈΠΌΠ΅Π΅Ρ ΡΠ²ΠΎΠΉ Π½Π΅Π·Π°Π²ΠΈΡΠΈΠΌΡΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ.
Π ΠΏΡΠΈΠ²Π΅Π΄Π΅Π½Π½ΠΎΠΌ Π½ΠΈΠΆΠ΅ ΠΊΠΎΠ΄Π΅ ΠΏΠ΅ΡΠ΅Π΄Π°ΠΉΡΠ΅ ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΠΏΠΎΠ»ΠΎΡΡ ΠΏΡΠΎΠΊΡΡΡΠΊΠΈ ΠΈ ΡΠΊΠ°ΠΆΠΈΡΠ΅ ΡΠΎΡΠΊΠΈ ΠΏΡΠΎΠΊΡΡΡΠΊΠΈ.
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; >>
- ΠΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠ΅ ΠΏΡΠΎΠΊΡΡΡΠΊΠΈ Π²Π²Π΅ΡΡ ΡΡΡΠ°Π½ΠΈΡΡ / ΡΠ°Π·Π΄Π΅Π»Π° / Π΄Π΅Π»Π΅Π½ΠΈΡ Π½Π° Π²Π΅Π±-ΡΡΡΠ°Π½ΠΈΡΠ΅, ΠΏΠΎΠΊΠ° Π΅ΡΡΡ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»ΡΡΠΊΠ°Ρ ΠΏΠΎΠ»ΠΎΡΠ° ΠΏΡΠΎΠΊΡΡΡΠΊΠΈ (Π½Π΅ ΠΏΡΠΎΠΊΡΡΡΠΊΠ° Π±ΡΠ°ΡΠ·Π΅ΡΠ°). ΠΠ°ΠΆΠΌΠΈΡΠ΅ Π·Π΄Π΅ΡΡ. ΠΠ»Ρ Π΄Π΅ΠΌΠΎΠ½ΡΡΡΠ°ΡΠΈΠΈ ΠΈ ΠΏΡΠΎΠ²Π΅ΡΠΊΠΈ ΠΏΠΎΠ»ΠΎΡΡ ΠΏΡΠΎΠΊΡΡΡΠΊΠΈ ΠΈΠΌΠ΅Π΅Ρ ΡΠ²ΠΎΠΉ Π½Π΅Π·Π°Π²ΠΈΡΠΈΠΌΡΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ.
Π ΠΏΡΠΈΠ²Π΅Π΄Π΅Π½Π½ΠΎΠΌ Π½ΠΈΠΆΠ΅ ΠΊΠΎΠ΄Π΅ ΠΏΠ΅ΡΠ΅Π΄Π°ΠΉΡΠ΅ ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΠΏΠΎΠ»ΠΎΡΡ ΠΏΡΠΎΠΊΡΡΡΠΊΠΈ ΠΈ ΡΠΊΠ°ΠΆΠΈΡΠ΅ ΡΠΎΡΠΊΠΈ ΠΏΡΠΎΠΊΡΡΡΠΊΠΈ.
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; >>
- Π§ΡΠΎΠ±Ρ Π²ΡΠΏΠΎΠ»Π½ΠΈΡΡ ΠΏΡΠΎΠΊΡΡΡΠΊΡ Π²Π½ΠΈΠ·, ΠΊΠΎΠ³Π΄Π° Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΏΡΠΎΠΊΡΡΡΠΊΠΈ Π±ΡΠ°ΡΠ·Π΅ΡΠ° (ΠΡΡΡΠΎΠ΅Π½Π½ΡΠΉ Π±ΡΠ°ΡΠ·Π΅Ρ), ΠΈ Π²Ρ Ρ ΠΎΡΠΈΡΠ΅ ΠΏΡΠΎΠΊΡΡΡΠΈΡΡ Π²Π½ΠΈΠ· Ρ ΠΏΠΎΠΌΠΎΡΡΡ ΠΊΠ»Π°Π²ΠΈΡΠΈ Β«ΠΠ½ΠΈΠ·Β» . ΠΠ°ΠΆΠΌΠΈΡΠ΅ Π·Π΄Π΅ΡΡ Π΄Π»Ρ Π΄Π΅ΠΌΠΎΠ½ΡΡΡΠ°ΡΠΈΠΈ
Π ΠΏΡΠΈΠ²Π΅Π΄Π΅Π½Π½ΠΎΠΌ Π½ΠΈΠΆΠ΅ ΠΊΠΎΠ΄Π΅ ΠΏΠ΅ΡΠ΅Π΄Π°ΠΉΡΠ΅ ΡΠ²ΠΎΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΠΎΠ±Π»Π°ΡΡΠΈ ΠΏΡΠΎΠΊΡΡΡΠΊΠΈ, Π½Π°ΠΏΡΠΈΠΌΠ΅Ρ ΠΈ Π½Π°ΠΆΠΌΠΈΡΠ΅ ΠΊΠ½ΠΎΠΏΠΊΡ Β«Π²Π½ΠΈΠ·Β».
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; >>
- Π§ΡΠΎΠ±Ρ Π²ΡΠΏΠΎΠ»Π½ΠΈΡΡ ΠΏΡΠΎΠΊΡΡΡΠΊΡ Π²Π²Π΅ΡΡ , ΠΊΠΎΠ³Π΄Π° Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΏΡΠΎΠΊΡΡΡΠΊΠΈ Π±ΡΠ°ΡΠ·Π΅ΡΠ° (ΠΡΡΡΠΎΠ΅Π½Π½ΡΠΉ Π±ΡΠ°ΡΠ·Π΅Ρ), ΠΈ Π²Ρ Ρ ΠΎΡΠΈΡΠ΅ ΠΏΡΠΎΠΊΡΡΡΠΈΡΡ Π²Π²Π΅ΡΡ Ρ ΠΏΠΎΠΌΠΎΡΡΡ ΠΊΠ»Π°Π²ΠΈΡΠΈ 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; >>
- Π§ΡΠΎΠ±Ρ Π²ΡΠΏΠΎΠ»Π½ΠΈΡΡ ΠΏΡΠΎΠΊΡΡΡΠΊΡ Π²Π½ΠΈΠ·, ΠΊΠΎΠ³Π΄Π° Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΏΡΠΎΠΊΡΡΡΠΊΠΈ Π±ΡΠ°ΡΠ·Π΅ΡΠ° (ΠΡΡΡΠΎΠ΅Π½Π½ΡΠΉ Π±ΡΠ°ΡΠ·Π΅Ρ), ΠΈ Π²Ρ Ρ ΠΎΡΠΈΡΠ΅ ΠΏΡΠΎΠΊΡΡΡΠΈΡΡ Π²Π½ΠΈΠ· Ρ ΠΏΠΎΠΌΠΎΡΡΡ ΠΊΠ»Π°Π²ΠΈΡΠΈ Β«Π’ΠΎΠ»ΡΠΊΠΎ ΡΡΡΠ΅Π»ΠΊΠ° Π²Π½ΠΈΠ·Β» . ΠΠ°ΠΆΠΌΠΈΡΠ΅ Π·Π΄Π΅ΡΡ Π΄Π»Ρ Π΄Π΅ΠΌΠΎΠ½ΡΡΡΠ°ΡΠΈΠΈ
Π ΠΏΡΠΈΠ²Π΅Π΄Π΅Π½Π½ΠΎΠΌ Π½ΠΈΠΆΠ΅ ΠΊΠΎΠ΄Π΅ ΠΏΠ΅ΡΠ΅Π΄Π°ΠΉΡΠ΅ ΡΠ²ΠΎΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΠΎΠ±Π»Π°ΡΡΠΈ ΠΏΡΠΎΠΊΡΡΡΠΊΠΈ, Π½Π°ΠΏΡΠΈΠΌΠ΅Ρ ΠΈ Π½Π°ΠΆΠΌΠΈΡΠ΅ ΠΊΠ»Π°Π²ΠΈΡΡ Β«Π²Π½ΠΈΠ·Β».
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; >>
- Π§ΡΠΎΠ±Ρ Π²ΡΠΏΠΎΠ»Π½ΠΈΡΡ ΠΏΡΠΎΠΊΡΡΡΠΊΡ Π²Π²Π΅ΡΡ , ΠΊΠΎΠ³Π΄Π° Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΠΏΡΠΎΠΊΡΡΡΠΊΠΈ Π±ΡΠ°ΡΠ·Π΅ΡΠ° (ΠΡΡΡΠΎΠ΅Π½Π½ΡΠΉ Π±ΡΠ°ΡΠ·Π΅Ρ), ΠΈ Π²Ρ Ρ ΠΎΡΠΈΡΠ΅ ΠΏΡΠΎΠΊΡΡΡΠΈΠ²Π°ΡΡ Π²Π²Π΅ΡΡ Ρ ΠΏΠΎΠΌΠΎΡΡΡ ΠΊΠ»Π°Π²ΠΈΡΠΈ Β«Π’ΠΎΠ»ΡΠΊΠΎ ΡΡΡΠ΅Π»ΠΊΠ° Π²Π²Π΅ΡΡ Β» . ΠΠ°ΠΆΠΌΠΈΡΠ΅ Π·Π΄Π΅ΡΡ Π΄Π»Ρ Π΄Π΅ΠΌΠΎΠ½ΡΡΡΠ°ΡΠΈΠΈ
Π ΠΏΡΠΈΠ²Π΅Π΄Π΅Π½Π½ΠΎΠΌ Π½ΠΈΠΆΠ΅ ΠΊΠΎΠ΄Π΅ ΠΏΠ΅ΡΠ΅Π΄Π°ΠΉΡΠ΅ ΡΠ²ΠΎΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΠΎΠ±Π»Π°ΡΡΠΈ ΠΏΡΠΎΠΊΡΡΡΠΊΠΈ, Π½Π°ΠΏΡΠΈΠΌΠ΅Ρ ΠΈ Π²Π²Π΅Π΄ΠΈΡΠ΅ ΡΡΠ΅Π±ΡΠ΅ΠΌΡΠΉ ΠΊΠ»ΡΡ.
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; >>
- Π§ΡΠΎΠ±Ρ Π²ΡΠΏΠΎΠ»Π½ΠΈΡΡ ΠΏΡΠΎΠΊΡΡΡΠΊΡ Π²Π²Π΅ΡΡ / Π²Π½ΠΈΠ· ΠΏΡΠΈ ΠΏΡΠΎΠΊΡΡΡΠΊΠ΅ Π±ΡΠ°ΡΠ·Π΅ΡΠ° (Π²ΡΡΡΠΎΠ΅Π½Π½ΡΠΉ Π±ΡΠ°ΡΠ·Π΅Ρ), ΠΈ Π²Ρ Ρ ΠΎΡΠΈΡΠ΅ ΠΏΡΠΎΠΊΡΡΡΠΈΠ²Π°ΡΡ Π²Π²Π΅ΡΡ / Π²Π½ΠΈΠ· ΡΠΎΠ»ΡΠΊΠΎ Ρ ΡΠΈΠΊΡΠΈΡΠΎΠ²Π°Π½Π½ΠΎΠΉ ΡΠΎΡΠΊΠΎΠΉ . ΠΠ°ΠΆΠΌΠΈΡΠ΅ Π·Π΄Π΅ΡΡ Π΄Π»Ρ Π΄Π΅ΠΌΠΎΠ½ΡΡΡΠ°ΡΠΈΠΈ
Π ΠΏΡΠΈΠ²Π΅Π΄Π΅Π½Π½ΠΎΠΌ Π½ΠΈΠΆΠ΅ ΠΊΠΎΠ΄Π΅ ΠΏΠ΅ΡΠ΅Π΄Π°ΠΉΡΠ΅ ΡΠ²ΠΎΡ ΡΠΎΡΠΊΡ ΠΏΡΠΎΠΊΡΡΡΠΊΠΈ. ΠΠΎΠ»ΠΎΠΆΠΈΡΠ΅Π»ΡΠ½ΠΎΠ΅ ΠΎΠ·Π½Π°ΡΠ°Π΅Ρ, ΡΡΠΎ Π²Π½ΠΈΠ· ΠΈ ΠΎΡΡΠΈΡΠ°ΡΠ΅Π»ΡΠ½ΡΠ΅ ΡΡΠ΅Π΄ΡΡΠ²Π° ΠΏΡΠΎΠΊΡΡΡΠΈΠ²Π°ΡΡΡΡ Π²Π²Π΅ΡΡ .
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; >>
- Π§ΡΠΎΠ±Ρ Π²ΡΠΏΠΎΠ»Π½ΠΈΡΡ ΠΏΡΠΎΠΊΡΡΡΠΊΡ Π²Π²Π΅ΡΡ / Π²Π½ΠΈΠ· ΠΏΡΠΈ ΠΏΡΠΎΠΊΡΡΡΠΊΠ΅ Π±ΡΠ°ΡΠ·Π΅ΡΠ° (Π²ΡΡΡΠΎΠ΅Π½Π½ΡΠΉ Π±ΡΠ°ΡΠ·Π΅Ρ), ΠΈ Π²Ρ Ρ ΠΎΡΠΈΡΠ΅ ΠΏΡΠΎΠΊΡΡΡΠΈΠ²Π°ΡΡ Π²Π²Π΅ΡΡ / Π²Π½ΠΈΠ·, ΡΡΠΎΠ±Ρ ΡΠ΄Π΅Π»Π°ΡΡ ΡΠ»Π΅ΠΌΠ΅Π½Ρ Π² Π²ΠΈΠ΄ΠΈΠΌΠΎΠΉ ΠΎΠ±Π»Π°ΡΡΠΈ ΠΈΠ»ΠΈ Π΄ΠΈΠ½Π°ΠΌΠΈΡΠ΅ΡΠΊΠΎΠΌ ΠΏΡΠΎΠΊΡΡΡΠΊΠ΅ . ΠΠ°ΠΆΠΌΠΈΡΠ΅ Π·Π΄Π΅ΡΡ Π΄Π»Ρ Π΄Π΅ΠΌΠΎΠ½ΡΡΡΠ°ΡΠΈΠΈ
Π ΠΏΡΠΈΠ²Π΅Π΄Π΅Π½Π½ΠΎΠΌ Π½ΠΈΠΆΠ΅ ΠΊΠΎΠ΄Π΅ ΠΏΠ΅ΡΠ΅Π΄Π°ΠΉΡΠ΅ ΡΠ²ΠΎΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ.
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; >>
ΠΡΠΈΠΌΠ΅ΡΠ°Π½ΠΈΠ΅. ΠΠΎΠΆΠ°Π»ΡΠΉΡΡΠ°, ΠΏΡΠΎΠ²Π΅ΡΡΡΠ΅ ΡΠ²ΠΎΠΉ ΡΠ»ΡΡΠ°ΠΉ ΠΈ ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠΉΡΠ΅ ΠΌΠ΅ΡΠΎΠ΄Ρ. ΠΡΠ»ΠΈ ΠΊΠ°ΠΊΠΎΠΉ-Π»ΠΈΠ±ΠΎ ΡΠ»ΡΡΠ°ΠΉ ΠΎΡΡΡΡΡΡΠ²ΡΠ΅Ρ, Π΄Π°ΠΉΡΠ΅ ΠΌΠ½Π΅ Π·Π½Π°ΡΡ.
Python Selenium β How to Scroll Down a Page?
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.
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: