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()
4

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.