Skip to content
Related Articles

Related Articles

Scroll Web Page Base On Pixel Method Using Selenium in Python
  • Last Updated : 04 Jan, 2021

Selenium is a powerful tool for controlling web browsers through programs and performing browser automation. It is functional for all browsers, works on all major OS and its scripts are written in various languages i.e Python, Java, C#, etc, we will be working with Python.

A Scrollbar is helped you to circulate round display in vertical route if the modern-day web page scroll does now no longer the seen place of the display. It is used to transport the window up and down. Selenium Webdriver does now no longer requires scroll to carry out moves because it manipulates DOM. But in positive internet pages, factors best emerge as seen as soon as the person has scrolled to them. In such instances, scrolling can be necessary.

Requirements:

  • selenium
  • You need to install chromedriver and set the path. Click here to download. 

Step-by-step Approach:

Step 1: Import required modules



Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

from selenium import webdriver
import time
from webdriver_manager.chrome import ChromeDriverManager
  
# create instance of Chrome webdriver
driver=webdriver.Chrome(ChromeDriverManager().install())

chevron_right


Step 2: Taking any URL.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

from selenium import webdriver
import time
from webdriver_manager.chrome import ChromeDriverManager
  
# create instance of Chrome webdriver
driver=webdriver.Chrome(ChromeDriverManager().install())
  
#url

chevron_right


Step 3: Maximize the window.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

driver.maximize_window()

chevron_right


Step 4: Scrolling base on the pixel.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Below is the full Implementation:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

from selenium import webdriver
import time
from webdriver_manager.chrome import ChromeDriverManager
  
# create instance of Chrome webdriver
driver=webdriver.Chrome(ChromeDriverManager().install())
  
#url
  
#maximize window
driver.maximize_window()
  
#scroll by pixcel
driver.execute_script("window.scrollBy(0,2000)","")
time.sleep(4)

chevron_right


Output:


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :