我使用Selenium和Python將請求發送到網站。我現在正在進行文件上傳,但它看起來不像我預期的那樣工作。python文件上傳硒
這裏是如何HTML如下:
<div id="uploaders" class="uploaders" data-image-urls="" data-image-ids="" data-image-positions="" data-image-checksums=""><div id="uploader-container-0" class="uploader-container small empty" data-uploader-index="0" data-numbering="1" style="position: relative;">
<div id="file-picker-0" class="uploader-box small" style="position: relative; z-index: 1;">
<div class="thumb">
<div class="uploader-overlay">
<span class="photo-action edit-action">
<span class="mp-Icon-circle"><span class="mp-Icon mp-svg-edit photo-action-icon"></span></span>
</span>
<span class="remove photo-action">
<span class="mp-Icon-circle"><span class="mp-Icon mp-svg-delete photo-action-icon"></span></span>
</span>
</div>
</div>
<div class="content">
<div class="mp-svg-button-camera camera-logo centered"></div>
<div class="main-photo-subtext centered">
Hoofdfoto.
</div>
</div>
</div>
<input type="hidden" name="images.urls" value="">
<input type="hidden" name="images.ids" value="">
<input type="hidden" name="images.checksums" value="">
<div id="html5_1bdvv7bg0ptl15m6gmu1d4v16l04_container" class="moxie-shim moxie-shim-html5" style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; overflow: hidden; z-index: 0;">
<input id="html5_1bdvv7bg0ptl15m6gmu1d4v16l04" type="file" style="font-size: 999px; opacity: 0; position: absolute; top: 0px; left: 0px; width: 100%; height: 100%;" multiple="" accept="image/jpeg,.jpg,.jpeg,image/bmp,.bmp,image/png,.png"></div>
正如你可以在代碼的底部看到有一個輸入類型的文件元素。但沒有形式。網站上的文件上傳工作如下。您點擊(+)號。然後我選擇對話框文件選擇文件。然後會出現另一個彈出窗口,如果您想接受某些內容,請用按鈕點擊。
我的問題是如何上傳文件。直到我還沒有做到這一點。
我的代碼:
upload_photo_field_xpath = ".//*[@id='uploader-container-0']/div/input"
upload_photo_element = WebDriverWait(driver, 10).until(
lambda driver: driver.find_element_by_xpath(upload_photo_field_xpath))
image = os.path.join(os.getcwd(), 'images/' + 'img.jpg')
print(image)
upload_photo_element.send_keys(image)
upload_photo_element.submit()
如何解決這個問題有什麼想法?
謝謝。
感謝您的回覆@Shoaib Akhtar。我會試試這個。有一個問題,但是這個解決方案與Windows相關,或者我可以在Mac上使用它。 – user3138008
通過此鏈接一次1)http://stackoverflow.com/questions/9233461/pywin32-com-objects-on-mac 2)http://stackoverflow.com/questions/13852864/using-python-with-腳本化的應用程序上-A-MAC –