White
White

Reputation: 13

How can I scroll page in php-webdriver?

I'm trying to get some screenshot using php-driver. And it appears that despite taking the picture of the entire web page, it just taking the picture that appears on the monitor/screen (that's why we call it screenshot).

So my question is how to capture a picture that located in the bottom of the page? Do we scroll the page to the specified element? or there is a way to take the picture of the entire page?

This is my screenshot code:

    $host = 'http://localhost:4444/wd/hub'; // this is the default
    $capabilities = DesiredCapabilities::firefox();
    $webdriver = RemoteWebDriver::create($host, $capabilities, 5000);
    function find_image($url) {
        //Screenshot
        $GLOBALS["webdriver"]->get($url);
        $element = $GLOBALS["webdriver"]->findElement(WebDriverBy::cssSelector('#law > p > img'));
        $element_width = $element->getSize()->getWidth();
        $element_height = $element->getSize()->getHeight();
        $element_x = $element->getLocation()->getX();
        $element_y = $element->getLocation()->getY();
        $screenshot = __DIR__ . "/number/" . count($GLOBALS["data"]) . ".png";
        $GLOBALS["webdriver"]->takeScreenshot($screenshot);
        $src = imagecreatefrompng($screenshot);
        $dest = imagecreatetruecolor($element_width, $element_height);

        imagecopy($dest, $src, 0, 0, $element_x, $element_y, $element_width, $element_height);
        imagepng($dest, $screenshot);
        return convert_image($screenshot);
    }

Upvotes: 1

Views: 6243

Answers (1)

Nigel Ren
Nigel Ren

Reputation: 57141

As you say, you can't take a screenshot of anything more than is on the current screen. But you can scroll the window to the bottom of the page.

$this->webDriver->executeScript('window.scrollTo(0,document.body.scrollHeight);');
$this->webDriver->takeScreenshot("a.png");

I use an instance variable for the webDriver, using GLOBALS isn't a particularly good way of using it (IMHO). You can pass it in as a variable, or use it as a parameter to a constructor if using a class.

Upvotes: 11

Related Questions