我正在制作一个应用程序(Windows 10和Selenium chromedriver),该应用程序在广告网站上为我制作了广告,其中包含所有详细信息,现在,我想开始为其上传图像的部分。我正在尝试类似的东西:
# Upload images
# Change the directory from the current running project's directory
# to the subsequent images folder
os.chdir(os.getcwd()+"/images")
# For each image that's in the folder
for file in os.getcwd():
# Click the upload button on the website to open up the windows
# directory viewer, and then select all the images it sees there?
driver.find_element_by_xpath('//*[@id="ImageUploadButton"]').send_keys(os.getcwd()+file)
# change directory back to the original os.getcwd()
os.chdir("..")
...试图适应我在SO上找到的另一个答案。我有程序可以运行到这一点,并且可以正常运行,但是可以通过显示以下目录窗口来完成:
...显然不是项目目录,也不是project / images文件夹目录。我什至不确定这是否是正确的解决方案,但这是我第一次尝试这样做。有任何想法吗?谢谢!
更新
根据要求,这是fileupload部分的html代码:
<h2>
<div class="number">4</div>
Media</h2>
<ul class="post-ad-layout">
<li class="jsonly">
<div id="MediaImageUpload" class="clearfix form-section placeholders">
<p class="images-title">Add at least one photo to complete your ad.</p>
<div class="images-content">
<h3>Add photos to attract interest to your ad</h3>
<div class="images-content-secondary">
<p>Include pictures with different angles and details. 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>
<p>Drag and drop to change the order of your pictures.</p>
</div>
</div>
<ol id="MediaUploadedImages">
</ol>
<span class="field-message" data-for="FileUploadInput"></span>
<div id="FileInputWrapper" class="file-input-wrapper">
<input type="hidden" name="file" class="fileErrorBox">
<div class="imageUploadButtonWrapper">
<button id="ImageUploadButton" type="button" class="button-update-cancel short file-upload-button">
Select Images</button>
</div>
</div>
</div>
</li>
答案 0 :(得分:1)
不在服务器上进行本地测试。
# get the button element
ele = driver.find_element_by_id("ImageUploadButton")
# add a hidden file input ( might have to change the onchange event based on the events associated to the button in above line as you don't have a form)
driver.execute_script("var x= document.createElement('INPUT');x.setAttribute('type', 'file'); x.setAttribute('onchange','this.form.submit()');x.setAttribute('hidden', 'true'); arguments[0].appendChild(x);",ele)
# send the picture path here ( this should upload the file)
driver.find_element_by_xpath("//input[@type='file']").send_keys("picture path should go here")