从元素丢失焦点而不重置浏览器的垂直滚动条

时间:2022-12-05 22:13:14

I have created a custom command that loses the focus from current web element, like this:

我创建了一个自定义命令,失去了当前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();
}

The problem is that, each time I call the LoseFocus() it sets the vertical scroll position to 0.
The vertical scroll bar should not change the current position. How do I achieve this? I have also tried using javascript only like this

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

but the same happens.

问题在于,每次调用LoseFocus()时,它都会将垂直滚动位置设置为0.垂直滚动条不应更改当前位置。我该如何实现这一目标?我也试过像这样的ExecuteScript使用javascript(null,“document.elementFromPoin(0,”“+ scrollPositionY.Value +”“)。click();”);但同样的事情发生了。

EDIT 1

At above c# code I'm getting the current vertical scroll bar position. Then I click on 'body' element on coordinates (0 , scroll pos. Y). I was doing that to prevent the scroll bar from changing its current Y coordinates, but still does not work.

在上面的c#代码我得到当前的垂直滚动条位置。然后我点击坐标上的'body'元素(0,滚动pos.Y)。我这样做是为了防止滚动条改变其当前的Y坐标,但仍然无效。

EDIT 2

Here is also a sample html sample html

这里还有一个示例html示例html

1 个解决方案

#1


0  

All logic that I was implemented was correct except I needed to click on offsetY not absoluteY coordinates, so no need to pass any element (in my case- document).

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

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

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

#1


0  

All logic that I was implemented was correct except I needed to click on offsetY not absoluteY coordinates, so no need to pass any element (in my case- document).

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

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

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