β

Selenium处理验证码

海运的博客 549 阅读
 

截取整个屏幕,获取验证码坐标及大小,然后再截取验证码:

<?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');
 
作者:海运的博客
个人笔记
原文地址:Selenium处理验证码, 感谢原作者分享。

发表评论