sharief has asked for the wisdom of the Perl Monks concerning the following question:

This is the source html tag <input type="file" name="ctl00$ContentPlaceHolderContent$FileUpload_op +fFile" id="ctl00_ContentPlaceHolderContent_FileUpload_opfFile" style= +"width:406px;" />
use strict; use warnings; use Win32::IEAutomation; my $ie = Win32::IEAutomation->new( visible => 1, maximize => 1, warnings => 1, ); $ie->gotoURL('http://www.example.com'); $ie->getTextBox('name:', 'ctl00$ContentPlaceHolderContent$FileUpload_o +pfFile')-> SetValue('C:\test\9780547076034NIMAS.opf'); $ie->WaitForDone(); $ie->closeIE();
There is no change to the textbox were we upload a file any help on this... I used all the possibilities like
$ie->getTextBox('name:', 'ctl00$ContentPlaceHolderContent$FileUpload_o +pfFile')->SetValue('C:\test\9780547076034NIMAS.opf'); $ie->getTextBox('name:', 'ctl00$ContentPlaceHolderContent$FileUpload_o +pfFile')->SetValue('C:/test/9780547076034NIMAS.opf'); $ie->getTextBox('name:', 'ctl00$ContentPlaceHolderContent$FileUpload_o +pfFile')->SetValue('C:\\test\\9780547076034NIMAS.opf'); $ie->getTextBox('name:', 'ctl00$ContentPlaceHolderContent$FileUpload_o +pfFile')->SetValue('C://test//9780547076034NIMAS.opf'); $ie->getTextBox('name:', 'ctl00$ContentPlaceHolderContent$FileUpload_o +pfFile')->SetValue("C:\test\9780547076034NIMAS.opf"); $ie->getTextBox('name:', 'ctl00$ContentPlaceHolderContent$FileUpload_o +pfFile')->SetValue("C:/test/9780547076034NIMAS.opf"); $ie->getTextBox('name:', 'ctl00$ContentPlaceHolderContent$FileUpload_o +pfFile')->SetValue("C:\\test\\9780547076034NIMAS.opf"); $ie->getTextBox('name:', 'ctl00$ContentPlaceHolderContent$FileUpload_o +pfFile')->SetValue("C://test//9780547076034NIMAS.opf");

Here is the html source file

<table cellpadding="1" cellspacing="1" border="0"> <tr> <td colspan="3"> <table cellpadding="1" cellspacing="1" style="width: 5 +00px; margin-left: 60px;"> <tr> <td colspan="3"> </td> </tr> <tr> <td class="label" colspan="3"> <label for='ctl00_ContentPlaceHolderConten +t_FileUpload_opfFile'><b>OPF file to auto-populate the metadata:</b>< +br /></label> <input type="file" name="ctl00$ContentPlac +eHolderContent$FileUpload_opfFile" id="ctl00_ContentPlaceHolderConten +t_FileUpload_opfFile" style="width:406px;" /></td> </tr> <tr> <td colspan="3"> &nbsp;<br /> <input type="submit" name="ctl00$ContentPl +aceHolderContent$btnPullMetadata" value="Validate &amp; Populate Fiel +ds Below" onclick="javascript:WebForm_DoPostBackWithOptions(new WebFo +rm_PostBackOptions(&quot;ctl00$ContentPlaceHolderContent$btnPullMetad +ata&quot;, &quot;&quot;, true, &quot;vgOPFValidation&quot;, &quot;&qu +ot;, false, false))" id="ctl00_ContentPlaceHolderContent_btnPullMetad +ata" title="Validate your OPF and pulling data out through OPF metada +ta" class="defaultButton" style="width:350px;" /><br />

Update: Dear monks i wrote a simple html file this is the file

<html> <head> </head> <body> File to upload: <INPUT TYPE=TEXT NAME="upfile"><BR> <INPUT TYPE=SUBMIT VALUE="Submit"> </body> </html>

This is my perl code

use strict; use Win32::IEAutomation; use warnings; my $ie = Win32::IEAutomation->new( visible => 1, maximize => 1); $ie->gotoURL('C:\ex.html'); $ie->getTextBox("name:", "upfile")->SetValue("C:\\qctool.jar"); $ie->WaitforDone;

The above code works for a normal textbox of type text now see the below code

<html> <head> </head> <body> File to upload: <INPUT TYPE=FILE NAME="upfile"><BR> <INPUT TYPE=SUBMIT VALUE="Submit"> </body> </html>

Use the same perl code

use strict; use Win32::IEAutomation; use warnings; my $ie = Win32::IEAutomation->new( visible => 1, maximize => 1); $ie->gotoURL('C:\ex.html'); $ie->getTextBox("name:", "upfile")->SetValue("C:\\qctool.jar"); $ie->WaitforDone;

Update: The $ie->getTextBox("name:", "upfile")->SetValue("C:\\qctool.jar");does not work for the input type = file. What shall i do to recover this

Note dear monks: IT seems there is an issue with upload file when the setvalue transfers the file path the OS does not recognizes it since the input type is file the OS accepts value only from the internal browse command but it skips the setvalue->()'s value. This happened with me in perl/tk too. When i used perl/tk $but = $frame_figurecheck1 -> Button (-text => "Browse", -command =>sub{getOpenFile()});when this line is executed in perl/tk the $but is not assigned with the selected value i mean the selected file path

Update: As thomas895 said to try this code

use diagnostics; if( my $textbox = $ie->getTextBox('name:', 'ctl00$ContentPlaceHolderCo +ntent$FileUpload_opfFile') ) { #Okay, now see if we can set the value if( $textbox->SetValue('C:\\test\\9780547076034NIMAS.opf') ) { print "it should work\n"; } else { print "could not set the value\n"; } } else { print "could not get the text box\n"; }

The output of my code was "could not set the value" any idea about this monks

Replies are listed 'Best First'.
Re: use Win32::IEAutomation;
by Khen1950fx (Canon) on Dec 26, 2011 at 13:26 UTC
    Win32::IEAutomation's documentation is simple and clear. I would do something like this:
    #!perl use strict; use warnings; use Win32::IEAutomation; my $ie = Win32::IEAutomation->new( visible => 1, maximize => 1, warnings => 1, ); $ie->gotoURL('http://www.example.com'); $ie->getTextBox('name:', "importFile")-> SetValue("C:\test\9780547076034NIMAS.opf"); $ie->WaitForDone(); $ie->closeIE();
    Make sure that you have Win32::OLE installed.

      Shouldn't the backslashes of the path in the SetValue() call be escaped since they're in a double-quoted string?

      Cheers,

      JohnGG

        Very much so. "\t" is a tab character, in that code. But backslashes often should be doubled even when they are inside a single-quoted string. That wouldn't be required for any of these particular backslashes (if the quotes were changed to single quotes), but it might be wise, depending on your style choices and programming habits.

        - tye        

Re: use Win32::IEAutomation;
by ww (Archbishop) on Dec 27, 2011 at 11:52 UTC

    Complete, original content of OP (revised only enough to render readably):

    Unable to reach the file using the code

    $ie->getTextBox(' name:', 'ctl00$ContentPlac eHolderContent $FileUploa +d_opfFil e')->SetValue('C :/test/ 9780547076034NIMAS .opf'); $ie->WaitforDone;

    Update: sharief: Please do NOT modify content without adding notes to make clear what's new. Also, please don't delete content; instead, use strikeouts to indicate what you felt you needed to remove.

      I am a newfolk can i please know how can i make my code clear to everyone in turn with my previous code if i wish to update my code means what should i do then...
        Reply to requests for clarification by adding a new node or editing the previous one.

        For example, you might have wanted to edit your OP to look like this:

        Unable to reach the file using the code

        $ie->getTextBox(' name:', 'ctl00$ContentPlac eHolderContent $FileUploa +d_opfFile')->SetValue('C :/test/9780547076034NIMAS.opf'); $ie->WaitforDone;

        Update: Now I've tried the code Khen1950fx gave me in the first reply below, but the output still isn't what I want. So I've tried these variations:

        $ie->getTextBox('name:', 'ctl00$ContentPlaceHolderContent$FileUpload_o +pfFile')->SetValue('C:\test\9780547076034NIMAS.opf'); $ie->getTextBox('name:', 'ctl00$ContentPlaceHolderContent$FileUpload_o +pfFile')->SetValue('C:/test/9780547076034NIMAS.opf'); $ie->getTextBox('name:', 'ctl00$ContentPlaceHolderContent$FileUpload_o +pfFile')->SetValue('C:\\test\\9780547076034NIMAS.opf'); ....

        What should I try next?

        End Update

Re: use Win32::IEAutomation;
by poj (Abbot) on Dec 27, 2011 at 10:32 UTC
    If $ContentPlaceHolderContent and $FileUpload_opfFile are variables then try
    $ie->getTextBox('name:', "ctl00$ContentPlaceHolderContent$FileUpload_o +pfFile")->SetValue('C:/test/9780547076034NIMAS.opf');
    poj
      <input type="file" name="ctl00$ContentPlaceHolderContent$FileUpload_opfFile" id="ctl00_ContentPlaceHolderContent_FileUpload_opfFile" style="width:406px;" /> This is the source html tag

        Using IE9 the SetValue method worked for that tag when I changed the type to "text". With type="file" I get a "Could not start AutoItX3 Control through OLE" error. It could be a security issue.

        poj
Re: use Win32::IEAutomation;
by Xiong (Hermit) on Dec 27, 2011 at 11:28 UTC

    When you asked about this in CB, I advised you to check out File::Spec. Obviously, you want to read the documentation of Win32::IEAutomation, too. I still hope this will be useful.

    Feste: Misprison in the highest degree. Lady, cucullus non facit monachum. That's as much to say as, I wear not motley in my brain....
Re: use Win32::IEAutomation;
by poj (Abbot) on Dec 29, 2011 at 12:59 UTC

    If you are stuck consider another route, something like this

    #!perl use strict; use WWW::Mechanize; my $url = "http://www.example.com"; my $field_name = 'ctl00$ContentPlaceHolderContent$FileUpload_opfFile'; my $file_name = 'c:/test/9780547076034NIMAS.opf'; my $mech = WWW::Mechanize->new(); $mech->get($url); $mech->submit_form( form_number => 1, fields => { $field_name => $file_name }, ); die unless ($mech->success);

    You may need to change the form number or change to form_name

    poj
Re: use Win32::IEAutomation;
by poj (Abbot) on Dec 28, 2011 at 15:46 UTC

    Try this HTML. The text type works but the file doesn't which suggests the problem is not with Perl but with security features in IE introduced since 2006 that prevents the value being preset.

    <html> <head> </head> <body> <INPUT TYPE="TEXT" NAME="upfile1" VALUE="c:\qctool.jar"/> <BR> <INPUT TYPE="FILE" NAME="upfile2" VALUE="c:\qctool.jar"/> </body> </html>
    poj
Re: use Win32::IEAutomation;
by thomas895 (Deacon) on Dec 29, 2011 at 10:11 UTC

    I am not sure, but you should try using a conditional to check if it returns a true value. You enabled the -w flag and use strict, I trust, so the below code may help with debugging:

    use diagnostics; if( my $textbox = $ie->getTextBox('name:', 'ctl00$ContentPlaceHolderCo +ntent$FileUpload_opfFile') ) { #Okay, now see if we can set the value if( $textbox->SetValue('C:\\test\\9780547076034NIMAS.opf') ) { print "it should work\n"; } else { print "could not set the value\n"; } } else { print "could not get the text box\n"; }

    Be advised that I am unfamiliar with this module, and the getTextBox and SetValue methods may operate in void context, causing this test to be useless.

    ~Thomas~

      Hello thomas895 as you said i tried your code and its Output is "could not set the value". I am confused like is that the error with the package or with the OS. I dont know how to solve this issue its a challenge for me but dont know how, were to start and what to start

        With the code I provided, did you try what you had originally tried(the many different ways of the filename)?
        With MSWin, the packacge developers don't always use a common type(backslash or forward slash).

        ~Thomas~