0

I need to click on elements that are in the DOM, but they don't show on browser page unless I scroll down to the bottom of the page to see it.

Is there a better way for me to do it?

The program will fail with "Message: unknown error: Element is not clickable at point" without line scroll_browser(driver) and it is fine once we scroll down before clicking.

import time
from selenium import webdriver

def scroll_browser(driver, destination_height=None):
    # Get scroll height
    if not destination_height:
        destination_height = driver.execute_script("return document.body.scrollHeight")

    while True:
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

        new_height = driver.execute_script("return document.body.scrollHeight")
        if new_height == destination_height:
            break
        destination_height = new_height

if __name__ == '__main__':
    driver = webdriver.Chrome()
    driver.get('https://wikimediafoundation.org/wiki/Home')
    time.sleep(2)
    link = '//a[text()="Terms of Use"]'
    time.sleep(2)
    #scroll_browser(driver)
    driver.find_element_by_xpath(link).click()
    time.sleep(2)
    driver.close()
Tom
  • 1,387
  • 3
  • 19
  • 30
jacobcan118
  • 7,797
  • 12
  • 50
  • 95
  • If You need to scroll to element be in center of browser You can check this question https://stackoverflow.com/questions/20167544/selenium-scroll-element-into-center-of-view – jadupl Oct 20 '17 at 20:53
  • Possible duplicate of [Scrolling to element using webdriver?](https://stackoverflow.com/questions/41744368/scrolling-to-element-using-webdriver) – Mangohero1 Oct 20 '17 at 20:53
  • 1
    There is an known issue in chromedriver 2.30 when click an element not in current viewport. As WebDriver specification said, if the element not in current viewport, the webdriver need to scroll it into current viewport before click/sendkeys on it. So Webdriver.exe of each browser should do that for us, rather than adding external code by our self. So check your chromedriver version is 2.30 or not. Try upgrade to 2.32 and later. – yong Oct 21 '17 at 00:28
  • Possible duplicate of [Selenium Web Driver & Java. Element is not clickable at point (36, 72). Other element would receive the click:](https://stackoverflow.com/questions/44912203/selenium-web-driver-java-element-is-not-clickable-at-point-36-72-other-el) – undetected Selenium Oct 21 '17 at 06:33

1 Answers1

1

Try to scroll to required element with below code:

if __name__ == '__main__':
    driver = webdriver.Chrome()
    driver.get('https://wikimediafoundation.org/wiki/Home')
    link = driver.find_element_by_link_text('Terms of Use')
    driver.execute_script('arguments[0].scrollIntoView();', link)
    link.click()
    driver.close()
Andersson
  • 51,635
  • 17
  • 77
  • 129