【问题标题】:How to scroll to element with Selenium WebDriver如何使用 Selenium WebDriver 滚动到元素
【发布时间】:2015-04-12 23:12:09
【问题描述】:

如何让 Selenium WebDriver 滚动到特定元素以将其显示在屏幕上。我尝试了很多不同的选择,但都没有运气。 这在 C# 绑定中不起作用吗?

我可以让它跳转到一个特定的位置,例如

((IJavaScriptExecutor)Driver).ExecuteScript("window.scrollTo(0, document.body.scrollHeight - 150)");

但我希望能够将其发送到不同的元素,而无需每次都给出确切的位置。

public IWebElement Example { get { return Driver.FindElement(By.Id("123456")); } }

例 1)

((IJavaScriptExecutor)Driver).ExecuteScript("arguments[0].scrollIntoView(true);", Example);

例 2)

((IJavaScriptExecutor)Driver).ExecuteScript("window.scrollBy(Example.Location.X", "Example.Location.Y - 100)");

我看的时候没有跳到页面下到元素,异常匹配元素正在离屏。

我在它之后添加了一个bool ex = Example.Exists(); 并检查了结果。 它确实存在(它是真的)。 它没有显示(因为它仍然在屏幕外,因为它没有移动到元素) 它没有被选中??????

有人看到成功 By.ClassName。 有谁知道在 C# 绑定中执行此 By.Id 是否有问题?

【问题讨论】:

  • 也许您可以尝试模拟“向下箭头”按键,直到异常消失。我知道这是在墙倒塌之前一直在撞墙,但如果你只是想让它以某种方式工作,它可能是一个解决方案?!
  • 您可以尝试一件事,使用 ExpectedConditions.visibilityOfElement 方法并将其作为 while 循环的条件,并在循环中将视图滚动 100、100 个坐标。
  • 在调用 javascript 之前,您确定 WebDriver 已找到您的元素吗?
  • webelement 已经声明(Driver.FindElement)。我原以为它会找到它,因为它可以在页面顶部找到所有其他元素,并且在到达该元素之前有几个等待。我会测试并再次更新。我想滚动直到找到元素,因为 Selenium Webdriver 不允许您对屏幕外的元素执行任何操作。我希望避免任何涉及仅跳跃固定数量的解决方案,因为页面大小会有所不同。
  • driver.FineElement(By.Id('id-of-ele')).Click() 不会成功吗?

标签: c# selenium selenium-webdriver selenium-chromedriver


【解决方案1】:

这对我有用:

var elem = driver.FindElement(By.ClassName("something"));
driver.ExecuteScript("arguments[0].scrollIntoView(true);", elem);

【讨论】:

  • By.ClassName 对于我正在寻找的东西来说不够独特。我尝试了上述方法,但 By.Id 对我不起作用。您是否建议问题出在 By.Id 上?您是否看到 By.ClassName 而不是 By.ID 的屏幕外滚动?
  • By.ClassName 只是查找 en 元素的一个例子。您可以使用任何您想要/需要的东西来查找元素。这里真正的魔力是滚动到发生在 JavaScript scrollIntoView() 方法中的元素。如果您很难找到元素,请尝试在此处发布 HTML。
  • 我刚刚测试了建议的解决方案,它正在工作。正如 user3734429 所说,您没有义务按类名进行过滤,您所需要的只是 WebElement(如何获取它取决于您,按类选择只是一种方式)。
【解决方案2】:

这适用于 Chrome、IE8 和 IE11:

public void ScrollTo(int xPosition = 0, int yPosition = 0)
{
    var js = String.Format("window.scrollTo({0}, {1})", xPosition, yPosition);
    JavaScriptExecutor.ExecuteScript(js);
}

public IWebElement ScrollToView(By selector)
{
    var element = WebDriver.FindElement(selector);
    ScrollToView(element);
    return element;
}

public void ScrollToView(IWebElement element)
{
    if (element.Location.Y > 200)
    {
        ScrollTo(0, element.Location.Y - 100); // Make sure element is in the view but below the top navigation pane
    }

}

【讨论】:

  • 哥们,你是最棒的!
  • 谢谢,这行得通! C#版本的ScrollTo()函数:IJavaScriptExecutor js = (IJavaScriptExecutor)driver;js.ExecuteScript(String.Format("window.scrollTo({0}, {1})", xPosition, yPosition));
【解决方案3】:

这是一个较老的问题,但我相信有比上面建议的更好的解决方案。

这是原始答案:https://stackoverflow.com/a/26461431/1221512

您应该使用 Actions 类来执行滚动到元素。

var element = driver.FindElement(By.id("element-id"));
Actions actions = new Actions(driver);
actions.MoveToElement(element);
actions.Perform();

【讨论】:

  • 这对我很有用,谢谢。我从来没有真正关注过Actions,但他们最近出现了几次。我真的应该更多地了解他们。
【解决方案4】:

为了在页面内向下滚动,我有一些小代码和解决方案

我的场景是直到我向下滚动页面。接受和不接受按钮未启用。我有 15 个条款和条件,我需要通过检查网页并获取最后一个条款和条件段落的 ID 来从中选择第 15 个条款和条件。

driver.FindElement(By.Id("para15")).Click();

<div id="para15">One way Non-Disclosure Agreement</div>

【讨论】:

    【解决方案5】:

    我为 IWebDriver 创建了一个扩展:

    public static IWebElement GetElementAndScrollTo(this IWebDriver driver, By by)
    {
        var js = (IJavaScriptExecutor)driver;
        try
        {
            var element = driver.FindElement(by);
            if (element.Location.Y > 200)
            {
                js.ExecuteScript($"window.scrollTo({0}, {element.Location.Y - 200 })");
            }
            return element;
        }
        catch (Exception ex)
        {
            return null;
        }
    }
    

    【讨论】:

    • 干得好!也为我工作!谢谢。
    【解决方案6】:

    这在 C# 自动化中对我有用:

    public Page scrollUp()
    {
        IWebElement s = driver.FindElement(By.Id("your_locator")); ;
        IJavaScriptExecutor je = (IJavaScriptExecutor)driver;
        je.ExecuteScript("arguments[0].scrollIntoView(false);", s);
        return this;
    }
    

    【讨论】:

      【解决方案7】:

      我遇到了同样的问题。我正在处理网页,需要单击子窗口上的按钮,该按钮默认位于屏幕下方。 这是我使用的代码并且它有效。 其实我只是模拟了一个鼠标拖放,将窗口向上移动了 250 点,这样按钮就在屏幕上。

      Actions action = new Actions(driver);
      action.DragAndDropToOffset(driver.FindElement(By.XPath("put an element path which **is in the screen now**, such as a label")), 0, -250);
      action.Build().Perform();
      

      【讨论】:

        【解决方案8】:

        如果我们放置时间的原因是长时间加载页面,我们放置它。就这样吧。

        ChromeOptions options = new ChromeOptions();
        var driver = new ChromeDriver(options);
        driver.Navigate().GoToUrl("https://www.w3schools.com/");
        Thread.Sleep(5000);
        driver.ExecuteScript("scroll(0,400)");
        HtmlDocument countriesDocument = new HtmlDocument();
        countriesDocument.LoadHtml(driver.PageSource);
        

        【讨论】:

        • 纯代码答案并不是特别有用。请添加一些关于此代码如何解决问题的说明。
        【解决方案9】:

        我正在提供在特定元素内滚动的解决方案,例如可滚动表格。

        // Driver is the Selenium IWebDriver
        IJavaScriptExecutor exec = (IJavaScriptExecutor) Driver;
        
        int horizontalScroll= direction == Direction.Right ? X : 0;
        int verticalScroll  = direction == Direction.Down  ? Y : 0;
        
        exec.ExecuteScript(
            "arguments[0].scrollBy(arguments[1], arguments[2])"
          , Self
          , horizontalScroll
          , verticalScroll);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-12-07
          • 2017-01-03
          • 1970-01-01
          • 2017-06-04
          • 1970-01-01
          • 1970-01-01
          • 2020-05-18
          • 1970-01-01
          相关资源
          最近更新 更多