Scroll Element into View with Selenium
Selenium 2 tries to scroll to the element and then click on it. This is because Selenium 2 will not interact with an element unless it thinks that it is visible.
Scrolling to the element happens implicitly so you just need to find the item and then work with it.
Scrolling to element using webdriver?
You are trying to run Java code with Python. In Python/Selenium, the org.openqa.selenium.interactions.Actions
are reflected in ActionChains
class:
from selenium.webdriver.common.action_chains import ActionChains
element = driver.find_element_by_id("my-id")
actions = ActionChains(driver)
actions.move_to_element(element).perform()
Or, you can also "scroll into view" via scrollIntoView()
:
driver.execute_script("arguments[0].scrollIntoView();", element)
If you are interested in the differences:
- scrollIntoView vs moveToElement
How to scroll UP to an element and click in selenium?
Up or Down to scrollIntoView()
an element you have to induce WebDriverWait for the visibilityOfElementLocated()
and you can use the following solution:
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("element"))));
Having trouble scrolling down to element in Selenium, python
You are using
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
which is basically, to scroll to the bottom of the page
.
Please use this :
driver.execute_script("arguments[0].scrollIntoView(true);", element)
element.click()
this will basically scroll into the element view and element will be available in Selenium view port.
Is there a way to scroll up a particular div element in a webpage using selenium with PYTHON?
Yes, there are multiple ways using which you could scroll to particular element
By using the move_to_element()
element = driver.find_element_by_xpath("_element_")
action = ActionChains(driver)
action.move_to_element(element).perform()
By scrollIntoView()
element = driver.find_element_by_xpath("_element_")
driver.execute_script("arguments[0].scrollIntoView(true);", element)
For reference check here
Related Topics
Check If Item Is in an Array/List
How to Account for Period (Am/Pm) Using Strftime
How to Set the Aspect Ratio in Matplotlib
Check If a String in a Pandas Dataframe Column Is in a List of Strings
How to Switch to the Active Tab in Selenium
Can Python Pickle Lambda Functions
What Does a for Loop Within a List Do in Python
How to Redirect Stdout and Stderr to Logger in Python
Python: Simple List Merging Based on Intersections
Python - Is a Dictionary Slow to Find Frequency of Each Character
Coalesce Values from 2 Columns into a Single Column in a Pandas Dataframe
Cleanest Way to Get Last Item from Python Iterator
Unpickling a Python 2 Object with Python 3