[selenium]selenium.common.exceptions.MoveTargetOutOfBoundsException的处理

在做selenium爬虫的时候,点击链接,发现总提示这个错误selenium.common.exceptions.MoveTargetOutOfBoundsException

后来查询,发现这可能是selenium在Firefox运行上的BUG,

ActionChains(self.firefox).move_to_element(text_wrap).click(text_wrap).perform()
move_to_element方法并没有像我们想象那样将FIREFOX滚动到我们的对象所在位置,因而,当我们要操作的element不在窗口显示范围时,就会报错。
然后在网上找了个简单粗暴的方法,滚动到我们的element
def _scorll_to_element_by_js(self, tag):
        # 滑动滚动条到某个指定的元素
        js4 = "arguments[0].scrollIntoView();"
        # 将下拉滑动条滑动到当前div区域
        driver.execute_script(js4, tag)

在调用ActionChains前,先调用一下这个函数,让窗口显示出你要处理的element,就不会报错了。

原文地址:https://www.cnblogs.com/aocshallo1/p/12549653.html