python - 如何使用 Selenium 和 python 向上滚动然后单击

标签 python selenium-webdriver

我需要在 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/

相关文章:

python - 在 Python 中将时间从 AM/PM 格式转换为军事时间

python - 使用 Selenium (Python) 从输出框中获取值

python - 使用 selenium 和 python 找到一种特殊的按钮

selenium-webdriver - 输入后验证 selenium Webdriver 的 sendKeys(input)

javascript - 无法使用 selenium webdriver 选择 ajax 下拉值

python - 带参数的 Web2py Controller ?

python - 在 Python 中访问列表

python - Python 内置 'compile' 的主要用途是什么?

python - 如何将y轴比例因子移动到y轴标签旁边的位置?

selenium - 执行 Selenium 显式等待时 Katalon 中的 GroovyCastException