【问题标题】:cannot scroll to a web element as it is covered by other element无法滚动到 Web 元素,因为它被其他元素覆盖
【发布时间】:2016-02-10 06:39:28
【问题描述】:

我正在使用 Java 和 Selenium 编写测试。目标应用程序有一些内页,我需要在其中一个页面中滚动到 Web 元素的顶部。我用过:

jse.executeScript("arguments[0].scrollTop", element);

但它不起作用,我也使用了scrollIntoView,但它不起作用,因为该元素被另一个元素覆盖。

【问题讨论】:

  • 您可能希望将页面标记添加到您的问题中。就目前而言,没有足够的信息来提供答案。
  • 我使用了scrollIntoView(false),它成功了
  • 这个问题的答案:stackoverflow.com/questions/35355643/…应该对你有帮助。

标签: java selenium selenium-webdriver


【解决方案1】:

更新刚刚看到你的评论,很高兴你能以更简洁的方式工作。


使前景中的元素不可见,然后使用 scrollIntoView()。

...类似jse.executeScript('arguments[0].style.visibility="hidden"', element)?

我的 Python 解决方案,它应该很容易翻译:

def makeInvisible(driver, element):
    driver.execute_script('arguments[0].style.visibility="hidden"', element)

我通常通过 xpath 获取元素,然后将其传递给该函数。您还可以使用以下等效项仔细检查是否隐藏了某些内容:

def isInvisible(driver, element):
    return driver.execute_script('return window.getComputedStyle(arguments[0]).display === \'none\'', element)

当然还要编写一个函数来使元素再次可见。 希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 2017-07-18
    • 1970-01-01
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-27
    相关资源
    最近更新 更多