我正在尝试通过Python Selenium上传图片,它之前有效,但现在他们随机生成按钮的ID。我尝试了不同的css路径,xpath使其工作,但我没有任何其他ideea。
HTML
<div id="ImageUpload" class="form-section" style="position: relative;">
<div class="file-input-wrapper">
<button id="ImageUploadButton" type="button" class="button-update-cancel short file-upload-button ">
Select Images</button>
<input type="hidden" name="file" id="FileUploadInput">
<span class="field-message" data-for="FileUploadInput"></span>
<p class="message"><strong>Get at least twice the number of replies by uploading images</strong><br>Max 10 images. File size can be 15 MB per image with max dimension 6000x4000. For bitmap(.bmp) images, max file size is 4MB.</p>
<p class="file-input-current file-uploading">Uploading...</p>
<ol id="UploadingImages"></ol>
<p class="image-select">Select a "MAIN" image :</p>
<ol id="UploadedImages">
</ol>
</div>
<input type="hidden" name="images">
<div id="flash_19b01203e1o5npfd1nulhk11f293_container" class="moxie-shim moxie-shim-flash" style="position: absolute; top: 5px; left: 0px; width: 99px; height: 21px; overflow: hidden;"><object id="flash_19b01203e1o5npfd1nulhk11f293" type="application/x-shockwave-flash" data="Moxie.cdn.swf" width="100%" height="100%" style="outline:0"><param name="movie" value="Moxie.cdn.swf"><param name="flashvars" value="uid=flash_19b01203e1o5npfd1nulhk11f293&target=moxie.core.EventTarget.instance.dispatchEvent"><param name="wmode" value="transparent"><param name="allowscriptaccess" value="always"></object></div></div>
<object id="flash_19b01203e1o5npfd1nulhk11f293" type="application/x-shockwave-flash" data="Moxie.cdn.swf" width="100%" height="100%" style="outline:0"><param name="movie" value="Moxie.cdn.swf"><param name="flashvars" value="uid=flash_19b01203e1o5npfd1nulhk11f293&target=moxie.core.EventTarget.instance.dispatchEvent"><param name="wmode" value="transparent"><param name="allowscriptaccess" value="always"></object>
和python代码
### IMAGE UPLOAD ###
img_path="C:\\1.jpg"
s = win32com.client.Dispatch('WScript.Shell')
time.sleep(3)
browser.find_element_by_id('#flash_19b01203e1o5npfd1nulhk11f293').click()
time.sleep(2)
s.SendKeys(img_path, 0)
time.sleep(2)
s.SendKeys("{ENTER}", 0)
有什么建议吗?
答案 0 :(得分:1)
如果id总是在变化,请寻找一种不同的识别元素的方法。
元素是否始终是<div id="ImageUpload"
元素中的第一个flash对象?如果是这样,试试这个:
browser.find_element_by_xpath("//div[@id='ImageUpload']//object[@type='application/x-shockwave-flash']").click()