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?
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
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