win
・右クリックコピーをシミュレート
最終手段。
・右クリック関数
Cefsharpの特権。
その他
・普通に新しいタブで開く 親切にhttml header content-type がapplication/downloadになっていれば可能
JavaScript
・download属性を使う。
https://www.famitsu.com/images/000/227/931/l_60f7cc374672e.jpg
https://tutsplus.github.io/download-attribute/download/acme-doc-2.0.1.txt
<a href ="https://www.famitsu.com/images/000/227/931/l_60f7cc374672e.jpg"target = "_blank" download>https://www.famitsu.com/images/000/227/931/l_60f7cc374672e.jpg</a><ahref="https://tutsplus.github.io/download-attribute/download/acme-doc-2.0.1.txt" download>https://tutsplus.github.io/download-attribute/download/acme-doc-2.0.1.txt</a>
ブロックされていてうまく行かない。Downloading resources in HTML5 a[download] may not work as expected | by Marvin jude | Charisol Community | Medium
dataurlかblob
・canvasの場合、base64encording
・img/srcの場合、base64encording こちらはかなり難しい
劣化するやつ
JavascriptExecutor js = (JavascriptExecutor) driver;String base64string = (String) js.executeScript("var c = document.createElement('canvas');" + " var ctx = c.getContext('2d');" + "var img = document.getElementsByTagName('img')[0];" + "c.height=img.naturalHeight;" + "c.width=img.naturalWidth;" + "ctx.drawImage(img, 0, 0,img.naturalWidth, img.naturalHeight);" + "var base64String = c.toDataURL();" + "return base64String;");String[] base64Array = base64string.split(",");String base64 = base64Array[base64Array.length - 1]; byte[] data = Base64.decode(base64);ByteArrayInputStream memstream = new ByteArrayInputStream(data);BufferedImage saveImage = ImageIO.read(memstream);ImageIO.write(saveImage, "png", new File("path"));
引用:https://stackoverflow.com/a/61061867/14819077
ちゃんとしたやつ
public string ImageUrlToDataUrl(IWebDriver driver, string imageUrl) { var js = new StringBuilder(); js.AppendLine("var done = arguments[0];"); // The callback from ExecuteAsyncScript js.AppendLine(@" function toDataURL(url, callback) { var xhr = new XMLHttpRequest(); xhr.onload = function() { var reader = new FileReader(); reader.onloadend = function() { callback(reader.result); } reader.readAsDataURL(xhr.response); }; xhr.open('GET', url); xhr.responseType = 'blob'; xhr.send(); }"); // XMLHttpRequest->FileReader->DataURL conversion js.AppendLine("toDataURL('" + imageUrl + "', done);"); // Invoke the function var executor = (IJavaScriptExecutor) driver; var dataUrl = executor.ExecuteAsyncScript(js.ToString()) as string; return dataUrl; }
引用
https://stackoverflow.com/a/58330753/14819077
・html header content-typeをapplication/downloadに変更
Chrome拡張を使う。うまく行かない…Chrome拡張以外の手段が????