无法向下滚动到 Selenium 中的元素,python

Having trouble scrolling down to element in Selenium, python

我试图让 Selenium 在页面上向下滚动,然后单击按钮(“Add+”下方右下角的箭头),但我不断收到 NoSuchElementException 错误.这是我尝试过的方法,但仍然没有任何效果。

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

element = driver.find_element_by_xpath('/html/body/div[7]/div/div/main/section[6]/main/div/main/div[2]/section[1]/p/button')
element.click()

感谢任何帮助。

您正在使用

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

这基本上是 scroll to the bottom of the page

请使用这个:

driver.execute_script("arguments[0].scrollIntoView(true);", element)
element.click()

这基本上会滚动到元素视图中,元素将在 Selenium 视图端口中可用。