C# Selenium - Finding Element On Continuously Growing Page

后端 未结 3 1073
再見小時候
再見小時候 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:20

    I started using the below which seems to be working nicely. Thanks for the help all.

               IWebElement bio = SeleniumDriver.FindElement(By.XPath("path"));
                    Actions actions = new Actions(SeleniumDriver);
                    actions.MoveToElement(bio);
                    actions.Perform();
    
    0 讨论(0)
  • 2020-12-20 02:26

    I am not sure if this will work but you can try getting the max height of your page using something like this:

    Math.max($(document).height(), $(window).height())
    

    Then you can scroll using js.ExecuteScript.

    I'd still use findElement after that to initate the click.

    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题