我需要在 python 中使用 selenium 单击按钮。这就是我所拥有的:
read_more_buttons = responses[0].find_elements_by_class_name("read-more")
if len(read_more_buttons) > 0:
read_more_buttons[0].click()
大多数情况下它工作正常,但有时页面底部会出现一个无法忽略的覆盖层。因此我会收到此错误:
[element] is not clickable at point (665.7333145141602,883.4666748046875) because another element <div class="raq-module js-raq-module"> obscures it
在调用 click()
之前,我尝试使用此代码向下滚动页面:
driver.execute_script("window.scrollTo(0, " + str(read_more_buttons[0].location["y"] + 120) + ")")
但是,我仍然收到同样的错误。似乎通过调用 .click()
元素会滚动到页面的最底部,即覆盖层的正下方。如何上移页面然后点击?
最佳答案
那些该死的叠加层!
在这里,我们尝试使用 JS
来滚动到 View ,然后点击:
read_more_buttons = responses[0].find_elements_by_class_name("read-more")
if len(read_more_buttons) > 0:
driver.execute_script("arguments[0].scrollIntoView(true);", read_more_buttons[0])
driver.execute_script("arguments[0].click()", read_more_buttons[0])
关于python - 如何使用 Selenium 和 python 向上滚动然后单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49867377/