【问题标题】:Why am I getting a MoveTargetOutOfBoundsException for coordinates within the viewport? How to fix?为什么我在视口内得到坐标的 MoveTargetOutOfBoundsException?怎么修?
【发布时间】:2020-09-22 04:58:22
【问题描述】:

使用以下代码:

elem = driver.find_element_by_xpath('/html/body/canvas')
from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(driver)
actions.move_to_element_with_offset(elem, 185, -35).click().perform()

我无法导航到画布元素的所需部分并收到此错误:

MoveTargetOutOfBoundsException: move target out of bounds
(Session info: chrome=85.0.4183.102)

我的移动目标绝对在视口内,无需滚动即可使其可点击。我正在使用 chromedriver,并使用画布的左上角作为move_to_element_with_offset() 的像素坐标的起点。有什么想法可以解决这个问题吗?我对在 python 中单击画布上指定点的任何解决方案感兴趣,不需要使用相同的方法。

【问题讨论】:

标签: python selenium selenium-webdriver html5-canvas selenium-chromedriver


【解决方案1】:

move_to_element_with_offset()

move_to_element_with_offset() 将鼠标移动指定元素的偏移量,其中偏移量相对于元素的左上角。

如果您打算移动(185, -35) 的偏移量,则从<canvas> 元素的左上角开始,其中:

  • 向右 185 个单位
  • 35 单位以上

将始终生成MoveTargetOutOfBoundsException


解决方案

您可以使用正偏移量而不是向上移动的负偏移量,如下所示:

actions.move_to_element_with_offset(elem, 35, 35).click().perform()

参考

您可以在以下位置找到相关的详细讨论:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多