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)
然后找到您的提交按钮并单击它.
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)
我为那些想要使用烦人的msofiledialogs的人添加了一个答案.这是saravanan提出的解决方案的工作方式,但更适合Python.
我在为一家公司工作的脚本遇到了类似的问题.我正在尝试为公司的客户上传文档,但由于他们的网站工作方式,我无法利用send_keys直接发送路径,因此我不得不依赖于msofiledialog.
您只需要通过cmd屏幕安装AutoIt https://pypi.python.org/pypi/PyAutoIt/0.3或"pip install -U pyautoit"
在脚本页面上键入"import autoit"
在脚本中弹出文件对话框之前键入以下内容:
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)
所有这些方法都不适用于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)
我正在使用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键。
感谢您的所有答案!很有帮助!
| 归档时间: |
|
| 查看次数: |
56771 次 |
| 最近记录: |