python selenium gets image verification code
PHP中文网
PHP中文网 2017-06-12 09:21:28
0
4
1596

I want to cache the image verification code locally, and then let the user see the image through the web service and manually type the code to log in.
Considering that the official operation is interface-less, selenium screenshots cannot be used.
In addition to selenium screenshot and right-click method, is there any other way?

Currently I think of using js to re-request the verification code into canvas and then toDataURL into png base64 code and output it to Dom and then use selenium to obtain it.
The following code test uses the login entrance verification code of the mobile mall.

$('body').append('<canvas id="CAVASIMG"></canvas>');
var img=new Image();
img.src="http://shop.10086.cn/i/authImg";
var d=document.getElementById("CAVASIMG");
var cxt=d.getContext("2d");
img.onload = function(){
    d.width = img.width;d.height = img.height;
    cxt.drawImage(img,0,0);
    console.log(d.toDataURL('png'));
};

If the URL requesting the verification code is from a different domain than the login URL, a cross-domain error will be reported.
You also need to test whether the verification code image obtained in this way still exists within the validity period of the current session.
How to solve the above cross-domain problem?

PHP中文网
PHP中文网

认证0级讲师

reply all(4)
学霸

You first need to capture the packet to see the request path of the image, and then use requests to download the image

漂亮男人

The method above is a method, you can also use selenium + PhantomJS

过去多啦不再A梦

The picture verification code and the cookies when reading the picture are integrated

Just make sure the verification code you answer is consistent with the cookies.

小葫芦

In the end I used the js method

var img=document.getElementById('IMGCODEID');
var d=document.createElement('CANVAS');
var cxt=d.getContext('2d');
d.width=img.width;d.height=img.height;
cxt.drawImage(img,0,0);
img.src=d.toDataURL('png');
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template