在做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,就不會報錯了。