【问题标题】:How can i upload files on dropzone.js with style="display: none;" using Selenium?如何使用 style="display: none;" 在 dropzone.js 上上传文件使用硒?
【发布时间】:2018-03-07 05:09:10
【问题描述】:

我正在尝试在使用 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>

【问题讨论】:

  • 你能用你观察到的错误堆栈跟踪更新问题吗?

标签: javascript python selenium selenium-webdriver dropzone.js


【解决方案1】:

根据您共享的 HTML&lt;input&gt; 标记的 style 设置为 display: 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")

【讨论】:

    【解决方案2】:

    DropzoneJS 隐藏了&lt;input type="file"&gt; 您可以通过以下说明找到那些:

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

    然后,例如,要将文件添加到页面上的第一个 dropzone,您应该按照以下步骤进行:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-01
      • 2013-12-21
      • 1970-01-01
      • 2019-12-30
      • 2021-09-05
      • 2014-10-16
      • 1970-01-01
      相关资源
      最近更新 更多