save an image with selenium & firefox

后端 未结 5 1998
灰色年华
灰色年华 2021-02-08 19:58

i\'m trying to save an image from a website using selenium server & python client. i know the image\'s URL, but i can\'t find the code to save it, either when it\'s the the

相关标签:
5条回答
  • 2021-02-08 20:35

    I was trying to accomplish the same task, but the images I wanted to grab were the size of my monitor (wallpaper) -- so the capture screenshot workaround didn't work for me. I figured out a way to do it...

    I've got selenium set up to go to the page I want (which induces all the session goodies) Then I used a program called "Workspace Macro" to loop through the selenium tasks.

    Grab it from here http://www.tethyssolutions.com/product.htm -- they have a trial version, which I think works for 30 runs or something.

    So here's the progression:

    • start firefox
    • open selenium and load test case
    • start it, but quickly pause it.
    • record a macro, which pushes "step" on selenium, then goes over to the firefox window and clicks file->save page as, saves, then stop recording
    • run the macro x times...
    • profit??

    Cheers

    0 讨论(0)
  • 2021-02-08 20:37

    I haven't used selenium, but if you know the image's URL, why not just do:

    from urllib import urlretrieve
    
    urlretrieve(url, filename)
    

    which will save the url to the filename. more info here

    0 讨论(0)
  • 2021-02-08 20:42

    I found code that puts an image in to a canvas, then converts it to data - which could then be base64 encoded for example. My thought was to call this using the eval command in selenium, however in my testing the toDataURL is throwing a security error 1000. Seems like it is so close to a solution if not for that error.

    var data, canvas, ctx;
    var img = new Image();
    img = document.getElementById("yourimageID");
    canvas = document.createElement('canvas');
    canvas.width = img.width;
    canvas.height = img.height;
    ctx = canvas.getContext("2d");
    ctx.drawImage(img, 0, 0);  // everything works up to here
    data = canvas.toDataURL();  // this fails ***
    var base64Img = data.replace(/^data:image\/(png|jpg);base64,/, "");
    

    Doing some research I found references that it is not allowed to use toDataURL when the image is from a different domain. However, I even tried this code by saving the page, stripping everything out except the image itself and this script.

    For example (index.html) :

    <html><head></head><body>
    <img src="local/hard/disk/img.jpg" id="yourimageID">
    <script>
    // script from above
    </script>
    </body></html>
    

    The img.jpg and index.html are stored locally, opening the page in firefox locally, still get a security error 1000!

    0 讨论(0)
  • 2021-02-08 20:42

    How about going to the image URL and then taking a screenshot of the page? Firefox displays the image in full screen. Hope this helps..

    0 讨论(0)
  • 2021-02-08 20:50

    To do this the way you want (to actually capture the content sent down to the browser) you'd need to modify Selenium RC's proxy code (see ProxyHandler.java) and store the files locally on the disk in parallel to sending the response back to the browser.

    0 讨论(0)
提交回复
热议问题