- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can I scroll a web page using selenium webdriver in python?
Sometimes we need to perform action on an element which is not present in the viewable area of the page. We need to scroll down to the page in order to reach that element.
Selenium cannot perform scrolling action directly. This can be achieved with the help of Javascript Executor and Actions class in Selenium. DOM can work on all elements on the web page with the help of Javascript.
Selenium can execute commands in Javascript with the help of the execute_script() method. For the Javascript solution, we have to pass true value to the method scrollIntoView() to identify the object below our current location on the page. We can execute mouse movement with the help of the Actions class in Selenium.
Example
Code Implementation with Javascript Executor.
import time from selenium import webdriver driver = webdriver.Chrome (executable_path="C:\chromedriver.exe") driver.get("https://www.tutorialspoint.com/index.htm") # identify element l= driver.find_element_by_xpath("//*[text()='About Us']") # Javascript Executor driver.execute_script("arguments[0].scrollIntoView(true);", l) time.sleep(0.4) driver.close
While working with Actions class to scroll to view, we have to use the moveToElement() method. This method shall perform mouse movement till the middle of the element.
Example
Code Implementation with Actions.
from selenium.webdriver import ActionChains from selenium import webdriver driver = webdriver.Chrome (executable_path="C:\chromedriver.exe") driver.get("https://www.tutorialspoint.com/index.htm") # identify element I=driver.find_element_by_xpath("//*[text()='About Us']") # action object creation to scroll a = ActionChains(driver) a.move_to_element(l).perform()
Output
- Related Articles
- How to scroll a Web Page using coordinates of a WebElement in Selenium WebDriver?
- How to Scroll Down or UP a Page in Selenium Webdriver?
- How to scroll the Page up or down in Selenium WebDriver using java?
- How to scroll down the page till page end in the Selenium WebDriver?
- How can I clear text of a textbox using Python Selenium WebDriver?
- How to scroll down using Selenium WebDriver with Java?
- How to scroll a specific DIV using Selenium WebDriver with Java?
- Save a Web Page with Python Selenium
- How to scroll up/down a page using Actions class in Selenium?
- How to scroll to element with Selenium WebDriver using C#?
- How can I handle multiple keyboard keys using Selenium Webdriver?
- How to obtain the page title using Selenium webdriver?
- Is it possible to scroll down in a webpage using Selenium Webdriver programmed on Python?
- How can I close a specific window using Selenium WebDriver with Java?
- How can I select date from a datepicker div using Selenium Webdriver?
