【问题标题】:Selenium moveToElement() to Selenide硒 moveToElement() 到硒
【发布时间】:2020-02-25 04:08:45
【问题描述】:

我从 Selenide 开始,想知道是否可以滚动到元素。在 Seleniu 中,代码如下所示:

    Actions actions = new Actions(getWebDriver());
    actions.moveToElement(loadMore).build().perform();

我需要滚动才能看到它,因为如果它在屏幕之外,它就无法点击。

【问题讨论】:

  • 很可能选择器不正确,请先在 DevTools 中检查。
  • 没有选择器是可以的。

标签: java selenium selenide


【解决方案1】:

Selenide 有scrollIntoView 方法,实现了JavaScript 的scrollIntoView 和滚动元素到特定位置:

// the top of the element will be aligned to the top.
$("").scrollIntoView(true).click();

// element will be aligned to the center. 
$("").scrollIntoView("{behavior: \"instant\", block: \"center\", inline: \"center\"}").click();

【讨论】:

    【解决方案2】:

    这还不够吗?

    actions.moveToElement(loadMore).click().build().perform()
    

    【讨论】:

      【解决方案3】:

      即使元素在屏幕之外,您也应该能够单击它(确保 XPath 正确!) 您可以使用 actions.scrollIntoView(actions.findElement(By.xpath(xpath))

      【讨论】:

      • 你确定吗?很多资源都说这可能是个问题。
      • 我已经在我现在的工作地点使用 Selenium 大约半年了,所以我还不能 100% 确定,但根据我的经验,如果找到该元素,浏览器会自动滚动到该元素当我尝试点击它时。我使用 driver.findElement(By.xapth(xpath)).click();请注意,在此之前我总是放置一个 driver.waitUntil(Expected condition.visibilityOfElementLocated(By.xpath(xpath)), 20000) (20 seconds timeout ) 以确保元素已加载/可见。希望能帮助到你!如果您还有其他问题,请告诉我!
      猜你喜欢
      • 2017-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-30
      • 2021-01-04
      • 2017-07-25
      • 2020-08-23
      相关资源
      最近更新 更多