【问题标题】:With Selenium on CentOS 7, getting a MoveTargetOutOfBoundsException on my CentOS 7, which doesn't occur when the script is run on Mac OS Big Sur在 CentOS 7 上使用 Selenium,在我的 CentOS 7 上获得 MoveTargetOutOfBoundsException,当脚本在 Mac OS Big Sur 上运行时不会发生
【发布时间】:2022-05-26 15:47:42
【问题描述】:

我正在使用带有最新 Selenium 的 Python 3.9 并拥有此代码,该代码在我的 Mac 上运行良好,Chrome 驱动程序 101 我的脚本的无头实例...

  element = self.driver.find_element(By.CSS_SELECTOR, "body")
  actions = ActionChains(self.driver)
  actions.move_to_element_with_offset(element, 0, 0).perform()

但是,当我在我的 CentOS 7 实例上运行相同的代码时,使用 chromedriver 99(最新可用),我收到此错误

>       raise exception_class(message, screen, stacktrace)
E       selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: move target out of bounds
E         (Session info: headless chrome=99.0.4844.84)

/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py:242: MoveTargetOutOfBoundsException

关于这意味着什么或我可能需要在我的 CentOS 7 设置上进行哪些额外配置的任何想法?只要代码在两种环境中都运行,就很高兴重写代码。

【问题讨论】:

  • 你能提供网页的网址吗?

标签: python-3.x selenium selenium-chromedriver macos-big-sur selenium-webdriver-python


【解决方案1】:

来自 MoveTargetOutOfBoundsException 的 Selenium 文档:

指示提供给动作 move() 方法的目标无效 - 超出窗口大小。

来自Actions 类文档:

moveByOffset​(int xOffset, int yOffset)
将鼠标从其当前位置(或 0,0)移动给定的偏移量。

这个问题对我来说看起来很奇怪,因为我们正在谈论 body 元素,并且不知何故它不在屏幕的可见部分, 你可以试试:

  1. 滚动到您的元素 - 使用 JavaScript 或 Selenium

    ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);

    1. 等到元素加载完毕
    2. 不同的偏移量 - 正/负。
    3. 获取窗口的滚动高度/宽度以及主体的 X 和 Y 坐标 - 这可以帮助您调试问题。

    document.body.scrollHeight

    window.innerHeight

    1. 您可以尝试先单击或悬停该元素。

    参考:
    How to get the browser viewport dimensions?
    https://www.lambdatest.com/automation-testing-advisor/selenium/classes/org.openqa.selenium.interactions.MoveTargetOutOfBoundsException
    Selenium - MoveTargetOutOfBoundsException with Firefox https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/WebElement.html#getLocation()

【讨论】:

  • 你知道等效语句“((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);"这对 Python 有用吗?我很难将您的答案翻译成可以放入 Python 脚本的内容。
  • driver.execute_script("arguments[0].scrollIntoView();", element) stackoverflow.com/questions/41744368/…
【解决方案2】:

move_to_element_with_offset方法用于将鼠标移动指定元素的偏移量。偏移量相对于元素的左上角。通常,当我们当时手动移动任何元素时,我们需要单击该元素然后移动它。希望这会有所帮助,如果没有,请告诉我。

  element = self.driver.find_element(By.CSS_SELECTOR, "body")
  actions = ActionChains(self.driver)
  actions.move_to_element_with_offset(element, 0, 0).click().perform()

【讨论】:

  • 试了一下,但仍然出现错误,“selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: move target out of bounds”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-09
  • 2015-06-29
  • 2016-04-20
  • 1970-01-01
  • 2018-07-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多