如何使用selenium,python上传文件(图片)

Fil*_*lip 48 python testing upload selenium file-upload

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

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

kar*_*kar 146

我正在做的是这个(确保drv是webdriver的一个实例):

drv.find_element_by_id("IdOfInputTypeFile").send_keys(os.getcwd()+"/image.png")
Run Code Online (Sandbox Code Playgroud)

然后找到您的提交按钮并单击它.

  • 您能否详细说明“os.getcwd()”的作用以及“image.png”在您的文件系统中的位置。 (2认同)
  • 当然![`os.getcwd()`](https://docs.python.org/2/library/os.html#os.getcwd)返回当前的工作目录.`image.png`位于同一目录中正在运行的脚本旁边. (2认同)
  • 这是做到这一点的完美方式。我应该指出表单需要有一个 ID,如果按钮也有,它会有所帮助! (2认同)

XRa*_*cat 15

控制诸如 Windows 文件选择器(或一般的操作系统)之类的组件的一种非常简单的方法是使用 pyautogui。可以通过pip安装pyautogui

import pyautogui
... # set the webdriver etc.
...
...
element_present = EC.presence_of_element_located((By.XPATH, "//button[@title='Open file selector']"))  # Example xpath

WebDriverWait(self.driver, 10).until(element_present).click() # This opens the windows file selector

pyautogui.write('C:/path_to_file') 
pyautogui.press('enter')
Run Code Online (Sandbox Code Playgroud)

  • 我不确定为什么没有给予更多选票。考虑到它也是跨平台的,它是迄今为止最优雅、最简单的解决方案。我花了几个小时努力寻找可行且简单的东西。我发现必须进行一些更改,因为“enter”选项未被识别。`pyautogui.write(文件路径,间隔=0.25) pyautogui.press('return')` (4认同)
  • 当在“无头”模式下使用 selenium(没有创建实际的浏览器 gui)时,这是否有效? (3认同)

Noc*_*sol 8

我为那些想要使用烦人的msofiledialogs的人添加了一个答案.这是saravanan提出的解决方案的工作方式,但更适合Python.

我在为一家公司工作的脚本遇到了类似的问题.我正在尝试为公司的客户上传文档,但由于他们的网站工作方式,我无法利用send_keys直接发送路径,因此我不得不依赖于msofiledialog.

  1. 您只需要通过cmd屏幕安装AutoIt https://pypi.python.org/pypi/PyAutoIt/0.3或"pip install -U pyautoit"

  2. 在脚本页面上键入"import autoit"

  3. 在脚本中弹出文件对话框之前键入以下内容:

    autoit.win_active("Open")autoit.control_send("Open","Edit1",r"C:\ Users\uu\Desktop\TestUpload.txt")autoit.control_send("Open","Edit1","{输入}")

它将查找打开的文件对话框窗口并填写它并按Enter键."打开"是我的文件对话框屏幕的标题.把你的头衔代替"开放".有更多创造性的方法来利用AutoIt的功能,但这对于初学者来说是一种简单,直接的方式.

编辑:不要.如果可以避免,请不要在大多数事情上使用control_send.它有一个众所周知的发送错误文本的问题.在我的情况下,我的文件路径中的冒号被转换为半冒号.如果您需要发送输入键,它应该没问题,但是如果您需要发送文本,请使用control_set_text.它具有相同的语法.

autoit.control_set_text("Open","Edit1",r"C:\Users\uu\Desktop\TestUpload.txt")
Run Code Online (Sandbox Code Playgroud)


sar*_*nan 7

所有这些方法都不适用于olx中的现代图像上传器!替代方法(仅适用于Windows)

1. Automation of windows can be done using Autoit 
2. Install Autoit and SciTe Script editor 
3. Autoit code :(imageupload.au3)

WinActivate("File Upload");  for chrome use "open" that is the name of the window that pops 
send("D:\images\image1.png");  path of the file
Send("{ENTER}")

4. compile it to get an .exe file(imageupload.exe)
5. Now from python call the .exe file like

import os
import os.system('C:\images\imageupload.exe') #path of the .exe file
Run Code Online (Sandbox Code Playgroud)


Ana*_*ana 6

我正在使用fine-uploader,用pytest运行硒测试,这对我有用:

    elm = driver.find_element_by_xpath("//input[@type='file']")
    elm.send_keys(os.getcwd() + "/tests/sample_files/Figure1.tif")
Run Code Online (Sandbox Code Playgroud)

在我的情况下,无需提交表单或Enter键。

感谢您的所有答案!很有帮助!


Ser*_*rov 5

上载输入控件将打开一个本机对话框(由浏览器完成),因此通过Selenium单击控件或浏览按钮将仅弹出对话框,并且测试将挂起。

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

请参阅C#中的示例问题,我确定还有一种在Python中调用JavaScript的方法,但我从未使用过Selenium Python绑定