C# Selenium - Finding Element On Continuously Growing Page

后端 未结 3 1074
再見小時候
再見小時候 2020-12-20 02:07

I am trying to find a better way to do this than using:

js.ExecuteScript(\"scroll(0, 1300)\");

I have a page where the data can change maki

3条回答
  •  离开以前
    2020-12-20 02:26

    I am going to take @ratsstack's answer little further and make it little easier for you to implement. I am not sure if there is a cleaner way to do this. But, at least something that should work. You can provide an estimated number of scroll you think is sufficient for the page to load fully. Notice you can provide more number than you think is correct. It does not really hurt or will not cause your script to fail.

    public void ScrollPage(int counter)
    {
        const string script =
            @"window.scrollTo(0,Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,document.documentElement.clientHeight));";
    
    
        int count = 0;
    
        while (count != counter)
        {
           IJavaScriptExecutor js = _driver as IJavaScriptExecutor;
            js.ExecuteScript(script);
    
            Thread.Sleep(500);
    
            count++;
        }
    }
    

    Note: I am using Thread.Sleep() to give the page sometime to finish loading. However, it is never recommanded to use hardcoded use. I am not sure about the technolgies you are using so really cannot provide any wait mechanism to wait for the page load. You can use something and replace the Thread.Sleep()

    Implementation

    ScrollPage(8);
    

提交回复
热议问题