如何用selenium、python上传文件(图片) [英] How to upload file ( picture ) with selenium, python

查看:82
本文介绍了如何用selenium、python上传文件(图片)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用selenium测试工具在web应用上传图片?我正在使用python.

How to upload a picture on a web application with the selenium testing tool? I am using python.

我尝试了很多东西,但没有任何效果.

I tried many things, but nothing worked.

推荐答案

上传输入控件会打开一个原生对话框(由浏览器完成)所以点击控件或通过 Selenium 浏览按钮只会弹出对话框,测试将挂起.

Upload input control opens a native dialog (it is done by browser) so clicking on the control or browse button via Selenium will just pop the dialog and the test will hang.

解决方法是通过 JavaScript 设置上传输入的值(在 Java 中通过 JavascriptExecutor 完成),然后提交表单.

The workaround is to set the value of the upload input via JavaScript (in Java it is done via JavascriptExecutor) and then submit the form.

请参阅这个问题以获取 C# 示例,我确信还有一种方法可以在Python,但我从未使用过 Selenium Python 绑定

See this question for sample in C#, I am sure there's also a way to call JavaScript in Python but I never used Selenium Python bindings

这篇关于如何用selenium、python上传文件(图片)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆