代码之家  ›  专栏  ›  技术社区  ›  Gian Marco Te

C#Selenium如何在不使用pageup/pgdn的情况下滚动可滚动的div,而不是整个网页

  •  0
  • Gian Marco Te  · 技术社区  · 10 年前

    我试图滚动一个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);
                    }
    
    2 回复  |  直到 10 年前
        1
  •  0
  •   Guy    10 年前

    您可以使用 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);
    }
    
        2
  •  0
  •   Gian Marco Te    9 年前

    修正了使用JavaScript,scrollTop函数。对元素使用scrollTop函数