0

I am using Selenium python to automate a site. The problem I have face is, I have to upload file but there is no input type file available where I could have been using send_keys() method. The File upload element:

<div id="data-assets-interior-file-upload" data-upload-properties="{&quot;formId&quot;:&quot;form-main-1&quot;,&quot;path&quot;:&quot;data[assets][interior]&quot;,&quot;modalUpload&quot;:&quot;Uploading...&quot;,&quot;warnExtensionsStrings&quot;:{&quot;pdf&quot;:&quot;&lt;div class=\&quot;a-box a-alert-inline a-alert-inline-warning\&quot;&gt;&lt;div class=\&quot;a-box-inner a-alert-container\&quot;&gt;&lt;i class=\&quot;a-icon a-icon-alert\&quot;&gt;&lt;\/i&gt;&lt;div class=\&quot;a-alert-content\&quot;&gt;\n            Most PDF files do not produce great results in an automated conversion process. We recommend using a Word, Mobi, ePUB or HTML file if you have one. &lt;a href=\&quot;\/en_US\/help\/topic\/A2GF0UFHIYG9VQ?ref_=_fg\&quot; target=\&quot;_blank\&quot; rel=\&quot;noopener noreferrer\&quot;&gt;Learn more.&lt;\/a&gt;\n         &lt;\/div&gt;&lt;\/div&gt;&lt;\/div&gt;\n         &lt;div id=\&quot;file-warn-actions\&quot; class=\&quot;a-form-actions a-spacing-none a-spacing-top-large\&quot;&gt;\n            &lt;div class=\&quot;a-row a-spacing-none\&quot;&gt;\n            &lt;div class=\&quot;a-column a-span6\&quot;&gt;\n               &lt;span class=\&quot;a-declarative\&quot; data-action=\&quot;potter-file-warn-extension-continue\&quot; data-potter-file-warn-extension-continue=\&quot;{}\&quot;&gt;\n                  &lt;span id=\&quot;file-warn-extension-continue\&quot; class=\&quot;a-button a-button-base button-fill\&quot;&gt;&lt;span class=\&quot;a-button-inner\&quot;&gt;&lt;button id=\&quot;file-warn-extension-continue-announce\&quot; class=\&quot;a-button-text\&quot; type=\&quot;button\&quot;&gt;\n                     Continue with PDF\n                  &lt;\/button&gt;&lt;\/span&gt;&lt;\/span&gt;\n               &lt;\/span&gt;\n            &lt;\/div&gt;\n            &lt;div class=\&quot;a-column a-span6 a-span-last\&quot;&gt;\n               &lt;span class=\&quot;a-declarative\&quot; data-action=\&quot;potter-file-warn-extension-cancel\&quot; data-potter-file-warn-extension-cancel=\&quot;{}\&quot;&gt;\n                  &lt;span id=\&quot;file-warn-extension-cancel\&quot; class=\&quot;a-button a-button-primary button-fill\&quot;&gt;&lt;span class=\&quot;a-button-inner\&quot;&gt;&lt;button id=\&quot;file-warn-extension-cancel-announce\&quot; class=\&quot;a-button-text\&quot; type=\&quot;button\&quot;&gt;\n                     I have another format\n                  &lt;\/button&gt;&lt;\/span&gt;&lt;\/span&gt;\n               &lt;\/span&gt;\n            &lt;\/div&gt;\n            &lt;\/div&gt;\n         &lt;\/div&gt;&quot;,&quot;pdf-header&quot;:&quot;Do you have another format?&quot;},&quot;acceptedExtensions&quot;:&quot;doc,docx,zip,htm,html,mobi,azw,epub,rtf,txt,pdf,kpf&quot;,&quot;multipart&quot;:null,&quot;persistSuccess&quot;:true,&quot;warnExtensions&quot;:[&quot;pdf&quot;],&quot;key&quot;:&quot;save&quot;,&quot;url&quot;:&quot;\/en_US\/title-setup\/kindle\/A3U1YUNVSBYTMZ\/content\/action\/save&quot;,&quot;workflowId&quot;:&quot;assets.interior&quot;,&quot;assetType&quot;:&quot;DIGITAL_BOOK_BLOCK&quot;}" class="a-section jele-file-field">
     <div class="a-section a-spacing-none file-upload-options-section">           
        
           <p class="a-spacing-small">                
           </p>               
              <div class="a-row file-upload-extra-info-message-section">
                 <div class="a-column a-span12">
                    <div class="a-box a-alert a-alert-info"><div class="a-box-inner a-alert-container"><i class="a-icon a-icon-alert"></i><div class="a-alert-content">Use Kindle Create to transform your manuscript to an eBook with professional book themes, images, and Table of Contents. <a href="https://kdp.amazon.com/help/topic/GHU4YEWXQGNLU94T?ref_=kdpgp_e_us_kc_tsw" target="_blank">Click here to download for free</a>.</div></div></div>
                 </div>
              </div>
              <br/>
          
           <div class="a-row file-upload-browse-section">
              <div class="a-column a-span12">                        
                <span class="a-declarative" data-action="browse-clicked" data-browse-clicked="{&quot;signInRequired&quot;:false,&quot;id&quot;:&quot;data-assets-interior-file-upload&quot;}">
                     <span id="data-assets-interior-file-upload-browse-button" class="a-button a-button-primary file-upload-browse-button"><span class="a-button-inner"><button id="data-assets-interior-file-upload-browse-button-announce" class="a-button-text" type="button">
                     Upload Book
                    </button></span></span>
                 </span>
                 <span class="a-declarative" data-action="file-selected" data-file-selected="{}" id="data-assets-interior-uploader">
                    <span class="fileuploader a-hidden"></span>
                 </span>
                 <p class="a-spacing-top-small a-size-mini a-color-tertiary a-text-italic">                        
                 </p>
              </div>
           </div>
           <input type="hidden" name="" value="doc,docx,zip,htm,html,mobi,azw,epub,rtf,txt,pdf,kpf" id="data-assets-interior-file-upload-accepted-extensions" class="accepted-extensions"/>            
 </div> </div>
     

Can anyone let me know, how to handle this scenario? If you are gonna recommend me some other library for it then please post relevant examples in python as well. Thank you

Gaj Julije
  • 1,538
  • 15
  • 32
Maha Waqar
  • 585
  • 1
  • 10
  • 24
  • When you click on upload button, do you see a Windows elements (like file explorer window)? – Gaj Julije Mar 18 '21 at 18:06
  • @Gaj Julije yes. It opens up file picker but send_keys() aren't working – Maha Waqar Mar 18 '21 at 18:57
  • Selenium do not interact wit windows elements. You can use AutoIT. See https://stackoverflow.com/a/44906824/11270766 And let me know does that help you. – Gaj Julije Mar 18 '21 at 19:04
  • at some point javsacript probably adds the tag. You can use Selenium to generate the events needed, and/or add the proper input tag, but what you do depends on the scripts used. If you keep the browser's inspector open while you manually browse for the file, you should see that tag added... that'll give you a clue on what to do. It may also fire an event after picking/accepting the file that submits the form and/or creates it's own AJAX call. (having said all that, AutoIT would make for a better test case) – pcalkins Mar 18 '21 at 20:30

0 Answers0