【问题标题】:Unable to upload image/file in python using selenium webdriver无法使用 selenium webdriver 在 python 中上传图像/文件
【发布时间】:2014-07-14 07:13:57
【问题描述】:

我无法上传和保存图片。

以下是有助于使用 UI 上传文件的一段代码。起初,只有上传按钮是可见的,一旦从上传器中选择了图像,保存按钮就会可见。

 <td colspan="1" class="actions">
                <input type="button" class="button" name="logo" id="logo-f-upload" value="Upload Image"/>
                <input type="file" id="logo-upload" accept="image/*" name="logo-upload" style="display:none" />
                <input type="submit" value="click to save" name="submit_logo_now" class="main submit_dm logo-save-focused" style="display: none"/>
                </br>
</td>

我试过driver.find_element_by_id("logo-f-upload").send_keys(os.getcwd()+"/image_test.png")

还有

driver.find_element_by_id("logo-upload").send_keys(os.getcwd()+"/image_test.png")

但是在这个阶段它不会抛出任何错误,但在下一个它说“元素不可见......”即。保存按钮。在 UI 上,模拟这个时,文件上传器没有打开,但 file_upload 按钮值更改为图像的路径,然后中断。

【问题讨论】:

  • 你看过this吗?
  • 我使用的是 Linux,输入类型不是 MULTI,所以我认为这不是解决方案。
  • 所以问题是“logo-upload”在屏幕上不可见,而 logo-f-upload 是,尽管 logo-upload 是 type = file 的输入。知道我现在能做什么吗?
  • dir(driver.find_element_by_id("logo-upload")) 说什么?也许输入标签有什么特别之处。
  • 是否需要在send_keys中添加回车符?

标签: python selenium file-upload selenium-webdriver


【解决方案1】:

我相信这只是一个 JS hack 的答案:所以问题是logo-upload 输入的样式设置为display:none,因此,selenium 无法找到该元素。对我有用的解决方案是使用 javascript 将样式设置为display:block,之后使用send_keys(img_path) 工作正常。

dr.execute_script("document.getElementById('logo-upload').setAttribute('Style','display:block')")

【讨论】:

    猜你喜欢
    • 2016-10-19
    • 1970-01-01
    • 1970-01-01
    • 2017-10-02
    • 1970-01-01
    • 2018-12-04
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    相关资源
    最近更新 更多