【问题标题】:C# Selenium how to scroll a scrollable div, not the entire webpage without using pageup/pgdnC# Selenium 如何在不使用 pageup/pgdn 的情况下滚动可滚动的 div,而不是整个网页
【发布时间】:2016-08-10 04:26:31
【问题描述】:

我正在尝试滚动一个 div,每次到达滚动条底部时都会加载行。现在我通过使用 PgUp 和 PgDn“滚动”div 的滚动条来做到这一点,但我遇到了一个问题,即特定 div 不接受 pgup/pgdown/home/end。如何使用 C# 和 Selenium 拖放滚动条?

这是我的 pgup 和 pgdn 代码:

private void ScrollTable(int scrollcount, string pageDirection, string delay)
                {
                    Actions actions = new Actions(DlkEnvironment.AutoDriver);
                    actions.MoveToElement(mTableElement);
                    for (int i = 1; i <= scrollcount; i++)
                    {
                       //if loading is not displayed/visible,
                       IsLoadingScreenIsDisplayed(delay);
                       //execute after waiting
                       switch (pageDirection.ToLower())
                       {
                           case "up":
                               actions.Click().SendKeys(Keys.PageUp).Perform();
                               break;
                           case "down":
                               actions.Click().SendKeys(Keys.PageDown).Perform();
                               break;
                           default:
                               throw new Exception("Invalid direction");
                       }   
                    }
                    Thread.Sleep(1000);
                }

【问题讨论】:

    标签: c# jquery html css selenium


    【解决方案1】:

    您可以使用SendKeys 而不将其发送到特定元素

    private void ScrollTable(int scrollcount, string pageDirection, string delay)
    {
        Actions actions = new Actions(DlkEnvironment.AutoDriver);
    
        for (int i = 1; i <= scrollcount; i++)
        {
            //if loading is not displayed/visible,
            IsLoadingScreenIsDisplayed(delay);
            //execute after waiting
            switch (pageDirection.ToLower())
            {
                case "up":
                    actions.SendKeys(Keys.PageUp).Perform();
                    break;
                case "down":
                    actions.SendKeys(Keys.PageDown).Perform();
                    break;
                default:
                    throw new Exception("Invalid direction");
            }
        }
    
        Thread.Sleep(1000);
    }
    

    【讨论】:

    • div 出于某种原因不接受 pgUp/pgDn 键,
    【解决方案2】:

    使用 JavaScript 修复,scrollTop 功能。对你的元素使用 scrollTop 函数

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-18
      • 1970-01-01
      • 1970-01-01
      • 2011-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多