Reputation: 9099
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()
Upvotes: 0
Views: 971
Reputation: 52685
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()
Upvotes: 1