如何查找仅在滚动后才可见的元素?

时间:2020-04-22 11:00:18

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

我有一个SAP应用程序,并且有一个对话框,如下所示:**enter image description here**

此对话框的html源代码为 enter image description here

我们可以看到只有18行 当我滚动条时(在图1中用蓝色箭头显示)html源代码 显示了另外18行,因为只有18行可以容纳在对话框中。 现在,我想将栏滚动到最后,然后选择框的最后一行。 我该怎么做?

我尝试使用

public void waitForElement()
        {
            //IJavaScriptExecutor je = (IJavaScriptExecutor)StaticDriver.driver;
            //IWebElement element = StaticDriver.driver.FindElement(By.XPath("(//*[@class= 'slick-cell l0 r0 row_62 cid_col_0']//div)//following-sibling::div"));
            //je.ExecuteScript("arguments[0].scrollIntoView(true);", element);

            var element = StaticDriver.driver.FindElement(By.XPath("(//*[@class= 'slick-cell l0 r0 row_62 cid_col_0']//div)//following-sibling::div"));
            Actions actions = new Actions(StaticDriver.driver);
            actions.MoveToElement(element);
            actions.Perform();
        }

// that xpath is the element of last row.

但是我遇到了错误Element count be found

如何滚动到最后一个并找到元素?

最后一个元素的View和html源代码为:

enter image description here

HTML源代码: enter image description here

2 个答案:

答案 0 :(得分:2)

尝试使用JavaScript执行器滚动到页面结尾:

 js.executeScript("window.scrollTo(0, document.body.scrollHeight)");

答案 1 :(得分:0)

上面的答案应该起作用,以防您要滚动到该元素时可以使用以下命令:-

IWebElement element = driver.FindElement(by);
((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].scrollIntoView(true);", element);