【问题标题】:Lose focus from element without resetting the browser's vertical scroll bar在不重置浏览器垂直滚动条的情况下从元素失去焦点
【发布时间】:2016-03-04 00:01:24
【问题描述】:

我创建了一个自定义命令,该命令会从当前 Web 元素中失去焦点,如下所示:

public void LoseFocus()
{
    int? scrollPositionY = Driver.Current.GetIntValueUsingScript("return $(document).scrollTop();");
    this.ClickOnElementsCoordinates(Driver.Current.FindElement(By.TagName("body")), 0, scrollPositionY.Value);
}

public void ClickOnElementsCoordinates(IWebElement element, int positionX, int positionY)
{
    var builder = new OpenQA.Selenium.Interactions.Actions(Driver.Current);
    builder.MoveToElement(element, positionX, positionY)
        .Click().Build().Perform();
    builder.Release();
}

问题在于,每次我调用LoseFocus() 时,它都会将垂直滚动位置设置为0。
垂直滚动条不应改变当前位置。我如何实现这一目标? 我也试过像这样使用javascript

ExecuteScript(null, "document.elementFromPoin(0,""+scrollPositionY.Value+"").click();");

但同样的事情也会发生。

编辑 1

在上面的 c# 代码中,我得到了当前垂直滚动条的位置。然后我单击坐标上的“body”元素(0,滚动位置 Y)。我这样做是为了防止滚动条改变其当前的 Y 坐标,但仍然不起作用。

编辑 2

这里也是一个示例 html sample html

【问题讨论】:

  • 你能移到另一个元素吗?
  • @lloyd 不幸的是,这不是一个选项。有需要等待ajax响应的情况(只有在元素外点击时才会发生)。
  • 那么你期望在哪里或指针移动到什么地方?
  • @lloyd 我希望它保持在当前垂直滚动条的位置,请参阅 Edit 1
  • 能否包含一个 html 示例部分?

标签: javascript c# selenium webdriver action


【解决方案1】:

我实现的所有逻辑都是正确的,除了我需要单击offsetY 而不是absoluteY 坐标,因此无需传递任何元素(在我的案例中 - 文档)。

// Wrong
builder.MoveToElement(element, posX, posY)

// Correct, clicking on document's offset
builder.MoveByOffset(posX, posY)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 1970-01-01
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    • 2016-04-28
    相关资源
    最近更新 更多