I have the following code which is supposed to scroll down the page and then click a button. When I run my script, I can see that the page does scroll until the element is at the very bottom of the page, but then the script fails when it gets time to click on that button and I get this error:
selenium.common.exceptions.ElementNotInteractableException: Message: Element could not be scrolled into view
I have tried using these methods:
- driver.execute_script(“arguments[0].scrollIntoView();”, element)
- driver.execute_script(“arguments[0].scrollIntoView();”, driver.find_element_by_xpath(xpath of element))
- actions.move_to_element(element)
All of these methods have the same result: the page will scroll to the element, but when it is time to click on the element it complains that the element could not be scrolled into view.
CODE:
hide_partial_rows_button_per_100 = driver.find_element_by_xpath('//button[@id="per_poss_toggle_partial_table"]') #driver.execute_script("arguments[0].scrollIntoView();",driver.find_element_by_xpath('//button[@id="per_poss_toggle_partial_table"]')) #driver.execute_script("arguments[0].scrollIntoView();",hide_partial_rows_button_per_100) actions.move_to_element(hide_partial_rows_button_per_100) actions.perform hide_partial_rows_button_per_100.click()
LINK TO PAGE I’M WORKING ON: https://www.basketball-reference.com/players/v/valanjo01.html
Someone on this site had a similar question but they were using JavaScript instead of Python and they added a time.sleep(1) between scrolling to the element and clicking on it, this did not work for me.
As said above, I’ve tried both the driver.execute_script(“arguments[0].scrollIntoView();”,element) and actions.move_to_element(element) methods and both work for scrolling, but when it is time to click on the element it complains that it cannot be scrolled into view.
Advertisement
Answer
You can apply location_once_scrolled_into_view
method to perform scrolling here.
The following code worked for me:
from selenium import webdriver from selenium.webdriver import DesiredCapabilities from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC options = Options() options.add_argument("start-maximized") options.add_argument('--disable-notifications') caps = DesiredCapabilities().CHROME caps["pageLoadStrategy"] = "eager" webdriver_service = Service('C:webdriverschromedriver.exe') driver = webdriver.Chrome(options=options, desired_capabilities=caps, service=webdriver_service) wait = WebDriverWait(driver, 20) url = "https://www.basketball-reference.com/players/v/valanjo01.html" driver.get(url) element = wait.until(EC.presence_of_element_located((By.XPATH, '//button[@id="per_poss_toggle_partial_table"]'))) element.location_once_scrolled_into_view