Fix error “Element is not clickable” in Selenium webdriver
我需要单击DOM中的元素,但是它们不会显示在浏览器页面上,除非向下滚动到页面底部才能看到。
有没有更好的方法让我去做?
如果没有行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 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() |
尝试滚动到所需元素,代码如下:
1 2 3 4 5 6 7 | 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() |