2016-11-10 76 views
1

我有點卡在如何克服這個障礙。我正在嘗試爲Kijiji製作一個轉貼,並且我在Mac上如何處理文件上傳。我試圖做Selenium上的圖片/文件上傳

driver.find_element_by_id('ImageUploadButton').send_keys(image) 

但是,這似乎並沒有做任何事情,我認爲,可能是因爲客齊集的特殊文件上傳的,但我不知道如何突破這個障礙。

有沒有人做過這個?

他們的「查看源文件」頁上的代碼:

<div id="ImageUpload" class="clearfix form-section placeholders"> 

    <p class="images-title">Add at least one photo. Use more to show different angles and details.</p> 
     <ol id="UploadedImages"> 
     </ol> 

    <span class="field-message" data-for="FileUploadInput"></span> 

      <div id="ImageDragAndDrop" class="clearfix"> 
       <div class="image"></div> 
       <div class="copy"> 
        <h3>Drag and Drop</h3> 
        <p>Drag and drop to change the order of your pictures.</p> 
       </div> 
      </div> 

      <div id="FileInputWrapper" class="file-input-wrapper"> 
       <input type="hidden" name="file" id="FileUploadInput" > 

       <h3>Get at least twice the number of replies by uploading images</h3> 
       <p>You can upload a maximum of <span id="MaxImages">10</span> photos, that are at least 300px wide or tall (we recommend at least 1000px).</p> 

       <button id="ImageUploadButton" type="button" class="button-update-cancel short file-upload-button"> 
        Select Images</button> 
      </div> 
     <input type="hidden" name="images"> 
    </div> 
+0

提供'HTML'代碼文件輸入元素 – Andersson

+0

這裏是它的屏幕截圖。 http://imgur.com/a/BRueF –

+0

我很確定你不能發送文本到'button'類型的元素。檢查你的頁面元素'>' – Andersson

回答

0

您需要爲目標的「文件」 input元素,而不是一個按鈕:

image_input = driver.find_element_by_id("FileUploadInput") 

現在的問題是,這元素是隱藏的,發送密鑰不會按原樣工作。爲了解決這個問題,就需要使元素可見第一:

driver.execute_script("arguments[0].type = 'file';", image_input) 
image_input.send_keys(image)