1

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.

Prophet
  • 32,350
  • 22
  • 54
  • 79

1 Answers1

0

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:\webdrivers\chromedriver.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
Prophet
  • 32,350
  • 22
  • 54
  • 79
  • Adding the element.location_once_scrolled_into_view has solved one error but now I'm getting this one: selenium.common.exceptions.ElementClickInterceptedException: Message: Element – maxpower8888 Nov 21 '22 at 20:52
  • EDIT: I was able to find the solution for my comment above, the element.location_once_scrolled_into_view did the trick for what was asked here, thank you! – maxpower8888 Nov 21 '22 at 21:10
  • So, as I understand your problem is now resolved or you still need some help? – Prophet Nov 21 '22 at 21:29
  • My problem is resolved, thank you. – maxpower8888 Nov 21 '22 at 21:58
  • Great! Nice to know that – Prophet Nov 21 '22 at 22:05