In this post we will learn ,How to scroll end of Page selenium Python.The Selenium web driver API is used for test automation on Windows or mobile, It helps us remotely control the different browsers automatically as if a real user is interacting by stimulating activities like opening or closing a new window, or tab, entering text in the textbox, select values from the dropdown, mouse control.
1. How to scroll end of Page selenium Python using scrollingElement
To scroll end of Pange in selenium Python, First, we will load a chrome browser driver with the help of ChromeDriverManager that automatically handles install of a new chrome driver , whenever the new version of the driver is released.The driver.maximize_window() method to maximize the opened browser web page.To navigate to “https://news.google.com/” used driver.get() method.
The execute_script() to execute Javascript Method.The scrollingElement
read-only property of the Document
interface returns a reference to the Element
that scrolls the document.We have used scrollingElement with scrollTop and scrollHeight to scroll end of Page
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.get("https://news.google.com/")
print("The First opened window = " + driver.title)
driver.execute_script("var scrollingElement = (document.scrollingElement || document.body);scrollingElement.scrollTop = scrollingElement.scrollHeight;")
time.sleep(3)
driver.close()
- How to scroll Page in Selenium Python
- How to Install selenium on windows Python
- How to install selenium in VSCode
- How to open chrome browser in selenium Python
- How to open Edge browser in selenium Python
- How to open Firefox browser in selenium Python
- Executable path has been deprecated in selenium Python
2. How to scroll end of Page selenium Python using end key
In this example,We will scroll to end of page with the help of find_element_by_tag_name() and send the end key to scroll end of page.
- Loading the chrome driver using ChromeDriverManager
- driver.get(): Navigate to a new link that is given as parameter. that is google news
- Next find element by tag name using selenium library method find_element_by_tag_name()
- Final step is SEND the END key to scroll end of Page
have first find the element by tag name using selenium library method find_element_by_tag_name()
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
import time
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.get("https://news.google.com/")
print("The First opened window = " + driver.title)
html_element = driver.find_element_by_tag_name('html')
html_element.send_keys(Keys.END)
time.sleep(3)
driver.close()
3. How to scroll end of infinite Page selenium Python
Sometimes we have to load a infinite web page,like facebook,instagram.To open any web page in selenium Python.First we have to load the web browser driver (chrome,Firefox,Edge) for test automation here loaded the chrome web browser driver.The driver.get() to open a link “https://news.google.com/”. The execute_script() to execute a javascript method to get the height and scroll to end of page infinite times.
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.get("https://news.google.com/")
print("The First opened window = " + driver.title)
last_Cal_height = driver.execute_script("return document.body.scrollHeight")
while True:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(1)
new_Cal_height = driver.execute_script("return document.body.scrollHeight")
if new_Cal_height == last_Cal_height:
break
Last_height = new_Cal_height
4. How to scroll end of Page selenium Python based on element
Sometimes we have to scroll web page until specific element is found.First we have found the element by using find_element_by_link_text() method .The Javascript execute_script() method to execute command,The first parameters and second agrument the element(Top_stories) based on that scrolling the web page. “The scrollIntoView()
method scrolls the element’s parent container such that the element on which scrollIntoView()
is called is visible to the user”
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.get("https://news.google.com/")
print("The First opened window = " + driver.title)
Top_stories = driver.find_element_by_link_text('Science')
driver.execute_script("arguments[0].scrollIntoView();",Top_stories)
time.sleep(2)
driver.close()
5.How to scroll to element selenium Python
In this example we have used The javascript scrollTo() that takes two agrument first is the initial starting point that is 0 and Second is the document.body.scrollHeight to scroll to the end of web page.
- Loading the chrome driver using ChromeDriverManager
- driver.get(): Navigate to a new link that is given as parameter
- execute_script() : This method is used to execute a javascript method window.scrollTo(0, document.body.scrollHeight)
- sleep(2): wait 3 seconds until it scroll down.
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import time
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.get("https://news.google.com/")
print("The First opened window = " + driver.title)
driver.execute_script("window.scrollBy(0,document.body.scrollHeight)")
time.sleep(2)
driver.close()