【问题标题】:Hover over element is working only once, using move_to_element().perform()悬停在元素上只工作一次,使用 move_to_element().perform()
【发布时间】:2020-09-17 10:47:58
【问题描述】:

我正在使用 Selenium,Python 将鼠标悬停在警告图标上并获取工具提示。我用过动作链。 悬停动作第一次成功。将鼠标悬停在另一个警告图标上时,相同的代码失败。 (如果我删除第一个悬停动作,则第二个悬停动作成功,即悬停动作只执行一次)。 我得到的错误:selenium.common.exceptions.StaleElementReferenceException:消息:过时的元素引用:元素未附加到页面文档

代码sn-p:

action = ActionChains(self.driver)
Warning_icon = self.driver.find_element_by_xpath("//div/i[@class='fa fa-exclamation-circle']")
assert Warning_icon 
action.move_to_element(Warning_icon) .perform()
TooltipMessage = self.driver.find_element_by_xpath("").text
action.release()

编辑1: 即使在 move_to_element(other_element) 中提到了其他元素,悬停也会发生在相同的警告消息上。 如何让鼠标悬停在新元素上? action.release() 似乎在这里不起作用。

【问题讨论】:

  • 虽然不是正确的方法,但我可以将鼠标悬停在不同图标上的唯一方法是声明“动作”,每次,我都必须悬停。 actions = ActionChains(self.driver).move_to_element(IconToHover) actions.perform()

标签: python selenium hover action


【解决方案1】:

也许问题如下:您将鼠标悬停在警告图标上并获得工具提示。之后,您会找到下一个元素并且鼠标正在尝试移动。此时,您当前的工具提示消失了,DOM 发生了变化,您得到了 StaleElementReferenceException。

解决方案:让我们在从一个图标移动到另一个图标之间将鼠标移动到另一个元素,或者尝试多次重复您的步骤。

【讨论】:

  • 为另一个元素尝试了 move_to_element.perform() 多次,然后将鼠标悬停在警告图标上,没有运气:(
猜你喜欢
  • 2015-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多