Python Selenium 图片上传 chromedriver

Python Selenium Image Upload chromedrive

我正在尝试通过 Python Selenium 上传图片,它以前可以工作,但现在它们会随机生成按钮的 ID。我已经尝试使用不同的 css 路径、xpath 来使其工作,但我没有任何其他想法。

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&amp;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&amp;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)

有什么建议吗?

如果 id 总是在变化,寻找识别元素的不同方式。

该元素是否始终是 <div id="ImageUpload" 元素内的第一个 Flash 对象?如果是这样,试试这个:

browser.find_element_by_xpath("//div[@id='ImageUpload']//object[@type='application/x-shockwave-flash']").click()