开发者

Get scrollheight cross-browser through Selenium

开发者 https://www.devze.com 2022-12-29 07:13 出处:网络
I\'m working on a project which is using Selenium and I\'d like to try to get the full web page height cross-browser and cross-platform. IE8 is being stubborn as always, is there anybody who has an id

I'm working on a project which is using Selenium and I'd like to try to get the full web page height cross-browser and cross-platform. IE8 is being stubborn as always, is there anybody who has an idea of how to solve this?

The problem: When you scroll down a page for e.g. 开发者_开发技巧500px and you keep doing this until the bottom of the page, the last scroll will be less than 500px. I need to know how much this last scroll was.

Two ways of solving: 1) Find the offset that has been scrolled each time (works everywhere except IE8) 2) Find the total height of the webpage

I know JQuery's height() function does this but I can't use this function from Selenium RC. If you know a way of calling JQuery functions through Selenium or any other solution, please tell!

Cheers, Henry


I've found a solution to my own problem. When you run tests with Selenium, it starts two windows: 1) The Selenium window executing all the commands 2) The Browser window in which the website is tested.

When you try to get info about window 2 via JavaScript functions, you need to do the following: selenium.browserbot.getCurrentWindow()

To get the full height of a browser window cross browser via selenium, you'll need following script:

function getPageHeight(){
    $scrOfY = 0;
    $test = $this->getEval("typeof(selenium.browserbot.getCurrentWindow().pageYOffset)");
    if(strcmp($test,"number") == 0) {
        //Netscape compliant
        $scrOfY = (int)$this->getEval("selenium.browserbot.getCurrentWindow().pageYOffset;");
        //scrOfX = window.pageXOffset;
    } else if( (bool)$this->getEval("selenium.browserbot.getCurrentWindow().document.body != null") && (bool)$this->getEval("selenium.browserbot.getCurrentWindow().document.body.scrollTop != null")) {
        //DOM compliant
        $scrOfY = (int)$this->getEval("selenium.browserbot.getCurrentWindow().document.body.scrollTop;");
        //scrOfX = document.body.scrollLeft;
    } else if( (bool)$this->getEval("selenium.browserbot.getCurrentWindow().document.documentElement != null") && (bool)$this->getEval("selenium.browserbot.getCurrentWindow().document.documentElement.scrollTop != null")) {
        //IE6 standards compliant mode
        $scrOfY = (int)$this->getEval("selenium.browserbot.getCurrentWindow().document.documentElement.scrollTop;");
        //scrOfX = document.documentElement.scrollLeft;
    }
    if(!$scrOfY || $scrOfY <= 0)
        $scrOfY = $this->getEval("selenium.browserbot.getCurrentWindow().document.body.offsetHeight");

    return $scrOfY;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消