我如何使用 Selenium 在 dropzone.js 上使用 style="display: none;" 上传文件?

How can i upload files on dropzone.js with style="display: none;" using Selenium?

我正在尝试在使用 dropzone.js 进行文件上传的网站上上传文件。 我在表单中找到了文件上传字段,但脚本停止时没有抛出任何错误。 我还使用 find_elements_by_id("upload-input") 来确保没有多个字段具有相同的 ID。

elem = driver.find_element_by_id("upload-input")
driver.implicitly_wait(2)
elem.send_keys("C:\KVR.pdf")

这是 html 的样子:

 <div id="fallback" style="display: none;">
    <input id="upload-input" type="file" name="file" multiple="multiple">
    <div id="upload-progress" class="upload-progress"></div>
  </div>

根据您分享的 HTML<input> 标签的 style 设置为 显示:none;。您可以使用以下代码块上传文件:

element = driver.find_element_by_xpath("//div[@id='fallback']")
driver.execute_script("arguments[0].removeAttribute('style')", element)
driver.find_element_by_xpath("//input[@id='upload-input']").send_keys("C:\KVR.pdf")

DropzoneJS 隐藏了 <input type="file"> 您可以按照以下说明找到它们:

 dz_inputs = driver.find_elements(By.XPATH, "//input[@type='file' and @class='dz-hidden-input']")

然后例如,要将文件添加到页面上的第一个拖放区,则应按以下步骤进行:

dz_inputs[0].send_keys("/File/path/file_name.extension")