截取整个屏幕,获取验证码坐标及大小,然后再截取验证码:
<?php
$element = $webDriver->findElement(
WebDriverBy::id('J-checkcode-img')
);
$element_width = $element->getSize()->getWidth();
$element_height = $element->getSize()->getHeight();
$element_src_x = $element->getLocation()->getX();
$element_src_y = $element->getLocation()->getY();
$webDriver->takeScreenshot('screenshot.png');
$src = \imagecreatefrompng('screenshot.png');
$dest = \imagecreatetruecolor($element_width, $element_height);
imagecopy($dest, $src, 0, 0, $element_src_x, $element_src_y, $element_width, $element_height);
imagepng($dest, './captcha.png');