Jump to content

WebDriver Image Download


Recommended Posts

I'm attempting to download files from PeopleSoft (specifically an image now) using the WebDriver. For other types of files, I click the link that downloads the files. With PDFs, I needed to add an option that changes the settings so that they're downloaded instead of viewed in a new tab. There doesn't appear to be an option for doing this with image files. Instead, I end up with a strange webpage that has HTML as follows:

<html style="height: 100%;">
    <head>
        <meta name="viewport" content="width=device-width, minimum-scale=0.1">
        <title>name_of_file.PNG (596×647)</title>
    </head>
    <body style="margin: 0px; background: #0e0e0e; height: 100%">
        <img 
            style="-webkit-user-select: none;margin: auto;background-color: hsl(0, 0%, 90%);transition: background-color 300ms;"
            src="www.companyname.com/really_long_url_that_matches_current_url/name_of_file.PNG"
        >
    </body>
</html>

The really confusing thing to me is that this webpage's URL exactly matches the image's src. (I wasn't sure, so I double checked to verify). As this is a temporary page, the URL also changes in between logins, so using WinHTTP won't be an option either (afaik). I have to be logged into the site to view anything, so using _WD_DownloadFile downloads an HTML page saying I need to log in.

I can't find another site like this to give as an example, but I'll be happy to provide as much information about this as I can :)

Edited by seadoggie01
Backslashes and forwardslashes... they're the same right?

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

It's part of a longer function, but here's where I discover it's a Png file, click it, and get a new tab...

; Other code...

; Click the attachment with a .PNG ending
_WD_ElementAction($__g_sSession, $aoAttachments[$i], "click")
If ErrMsg(_WD_ElementAction) Then Return SetError(1, @error, False)

; Really should check tab counts and wait for new tab, but it's not working yet anyways and this works for now
Sleep(5000)

; Get the handles
Local $aHandle = _WD_Window($__g_sSession, "handles")
If ErrMsg(_WDEx_Window) Then Return SetError(1, @error, False)

; Switch to the new tab (assume it's the second tab, always has been)
_WD_Window($__g_sSession, "switch", '{"handle":"' & $aHandle[1] & '"}')
If ErrMsg(_WDEx_Window) Then Return SetError(1, @error, False)

; Get the URL of the PNG
Local $sUrl2 = _WD_Action($__g_sSession, "url")
If ErrMsg(_WD_Action) Then Return SetError(1, @error, False)

; Setup the location to download the file
$sFile = _Voucher_DownloadPath() & "Voucher " & $sVoucherId & "." & $i & ".png"

; Download it (This actually downloads the a page as HTML that says "Please log in, blah blah blah")
_WD_DownloadFile($sUrl2, $sFile)
If ErrMsg(_WD_DownloadFile) Then Return SetError(1, @error, False)

; close the new tab and switch back to the orignal tab
_WD_Window($__g_sSession, "close")
If ErrMsg(_WDEx_Window) Then Return SetError(1, @error, False)

; Move to the original tab to continue downloading files
_WD_Window($__g_sSession, "switch", '{"handle":"' & $aHandle[0] & '"}')
If ErrMsg(_WDEx_Window) Then Return SetError(1, @error, False)

ErrMsg is a function the prints the function's name if there is an error

Edited by seadoggie01
Removed custom function _WDEx_Window

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

7 minutes ago, seadoggie01 said:

; Get the URL of the PNG

Looks like you are getting the webpage's URL. To get the image's URL, you need to --

  • Locate the IMG element
  • Retrieve its src attribute

Have you considered using _WD_Screenshot to grab the image?

9 minutes ago, seadoggie01 said:

_WDEx_Window

Can you describe what this function does beyond the standard _WD_Window?

Link to comment
Share on other sites

Right, the problem is that the .src attribute of the img element matches the URL of the webpage. I never tried it, but when inspecting the window I noticed this right away and didn't bother since the URL was easier to quickly grab.

_WDEx_Window was because I was annoyed with typing '{"handle":"' & $vHandle & '"}' for _WD_Window's switch and window options. :) I got lazy and made a wrapper because I don't like using single quotes or remembering things. (I tried to edit it out once I noticed, but wasn't fast enough)

Spoiler

 

Func _WDEx_Window($sSession, $sCommand, $sOption = Default)

    If IsKeyword($sOption) Then $sOption = ""
    $sCommand = StringLower($sCommand)

    Switch $sCommand
        Case "switch", "window"
            If $sOption <> "" Then $sOption = '{"handle":"' & $sOption & '"}'
    EndSwitch

    Local $vRet = _WD_Window($sSession, $sCommand, $sOption)
    Return SetError(@error, @extended, $vRet)

EndFunc

 

Edited by seadoggie01

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

18 minutes ago, Danp2 said:

Have you considered using _WD_Screenshot to grab the image?

Missed this first time around, sorry. No, I haven't previously. I suppose if I passed the img element to it, it would be good enough (for who it's for anyways :D)

I'll try this out and let you know! Thank you for your help!!

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

I read so carefully though! (squirrel!) Okay, I see... :D

Actually, even _WD_ElementActionEx doesn't work... this is nuts! If I right click the element and select "Save image as... " it tries to save the webpage (html) instead of the image. A screenshot of the page is the best I'll get it seems!

Either way, the quality of the image is nearly the same and the pixels match, so it's really quite good! Thank you again!!!

Edit: Apparently only Opera has issues with right clicking and saving the image, MS Edge does fine. I think I'm done for the week, lol

Edited by seadoggie01

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...