【发布时间】:2012-01-29 16:57:55
【问题描述】:
如何使用 selenium 测试工具在 web 应用程序上上传图片?我正在使用 python。
我尝试了很多东西,但没有任何效果。
【问题讨论】:
-
我在此处发布了 [使用 python 回答][1]。 [1]:stackoverflow.com/a/11872608/471376
标签: python testing file-upload selenium upload
如何使用 selenium 测试工具在 web 应用程序上上传图片?我正在使用 python。
我尝试了很多东西,但没有任何效果。
【问题讨论】:
标签: python testing file-upload selenium upload
上传输入控件会打开一个原生对话框(由浏览器完成),因此通过 Selenium 单击控件或浏览按钮只会弹出对话框,测试将挂起。
解决方法是通过 JavaScript 设置上传输入的值(在 Java 中是通过 JavascriptExecutor 完成的),然后提交表单。
请参阅this question 以获取 C# 中的示例,我确信还有一种方法可以在 Python 中调用 JavaScript,但我从未使用过 Selenium Python 绑定
【讨论】:
我正在做的是这个(确保 drv 是 webdriver 的一个实例):
drv.find_element_by_id("IdOfInputTypeFile").send_keys(os.getcwd()+"/image.png")
然后找到您的提交按钮并单击它。
【讨论】:
os.getcwd() 的作用以及在您的示例中image.png 在您的文件系统中的位置。
os.getcwd() 返回当前工作目录。 image.png 位于同一目录中正在运行的脚本旁边。
所有这些方法都不适用于 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
【讨论】:
我使用以下脚本格式上传图片。这可能会对您有所帮助。
Imagepath=os.path.abspath('.\\folder1\\subfolder2\file1.jpg')
driver.find_element_by_id("Id of the element").clear()
driver.find_element_by_id("Id of the element").send_keys(Imagepath)
如果您没有对象的 ID,则可以相应地使用 xpath 或 css 选择器。
【讨论】:
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shell.Sendkeys("C:\text.txt")
shell.Sendkeys("~")
将解决问题
【讨论】:
我正在使用 fine-uploader,使用 pytest 运行 selenium 测试,这对我有用:
elm = driver.find_element_by_xpath("//input[@type='file']")
elm.send_keys(os.getcwd() + "/tests/sample_files/Figure1.tif")
在我的情况下不需要表单提交或 Enter 键。
【讨论】:
使用分裂:
browser.attach_file('file_chooser_id',fully_qualified_file_path)
【讨论】:
我为任何想要处理烦人的 msofiledialogs 的人添加了一个答案。这是 saravanan 提出的解决方案,但更适合 Python。
我正在为一家公司编写脚本时遇到了类似的问题。我正在尝试为一家公司的客户上传文档,但由于他们网站的工作方式,我无法使用 send_keys 直接发送路径,所以我不得不依赖 msofiledialog。
您只需要安装 AutoIt https://pypi.python.org/pypi/PyAutoIt/0.3 或者只是通过 cmd 屏幕“pip install -U pyautoit”
在您的脚本页面上输入“import autoit”
在脚本中弹出文件对话框之前键入以下内容:
autoit.win_active("打开") autoit.control_send("打开","Edit1",r"C:\Users\uu\Desktop\TestUpload.txt") autoit.control_send("打开","Edit1","{ENTER}")
它将寻找打开的文件对话窗口并填写并按回车键。 “打开”是我的文件对话框屏幕的标题。将您的标题替换为“打开”。使用 AutoIt 的功能有更多创造性的方法,但这对于初学者来说是一种简单直接的方法。
编辑:不要。如果可以避免,请不要在大多数事情上使用 control_send。它有一个众所周知的发送错误文本的问题。在我的情况下,我的文件路径中的冒号被变成了一个半冒号。如果您需要发送输入键,应该没问题,但是如果您需要发送文本,请使用 control_set_text。它具有相同的语法。
autoit.control_set_text("Open","Edit1",r"C:\Users\uu\Desktop\TestUpload.txt")
【讨论】:
这是我使用的代码:
Imagepath = "C:\User\Desktop\image.png"
driver.find_element_by_xpath('//html/body/input').send_keys(Imagepath)
driver.find_element_by_xpath('//html/body/button').click()
我接受 karloskar 的回答。注意 它不适用于 FireFox (59)。它仅适用于 Chrome 驱动程序。
【讨论】:
使用autoit工具实现文件上传的完整代码。 你可以复制粘贴这个然后你就可以运行了,它会工作,因为它是一个实时演示。
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
import os
def fileUploading():
driver = webdriver.Firefox()
driver.implicitly_wait(20)
wait = WebDriverWait(driver, 10)
driver.get("https://demo.actitime.com/login.do");
driver.find_element(By.ID,"username").send_keys("admin")
driver.find_element(By.NAME, "pwd").send_keys("manager")
driver.find_element(By.XPATH, "//div[.='Login ']").click()
wait.until(ec.element_to_be_clickable((By.XPATH, "(//div[@class='popup_menu_icon'])[3]")))
driver.find_element(By.XPATH, "(//div[@class='popup_menu_icon'])[3]").click()
wait.until(ec.element_to_be_clickable((By.XPATH, "//a[contains(text(),'Contact actiTIME Support')]")))
driver.find_element(By.XPATH, "//a[contains(text(),'Contact actiTIME Support')]").click()
wait.until(ec.element_to_be_clickable((By.XPATH,"//div[@class='dz-default dz-message']")))
driver.find_element(By.XPATH,"//div[@class='dz-default dz-message']").click()
os.system("C:\\Users\\mallikar\\Desktop\\screenUpload.exe")
time.sleep(2000)
fileUploading()
下面是autoit代码的内容:
WinWaitActive("File Upload")
Send("D:\SoftwareTestingMaterial\UploadFile.txt")
Send("{ENTER}")
下载 autoIt 和 autoIt SCITE 编辑器工具。 完成后安装autoit并打开scite编辑器并粘贴上面的代码并以.au3扩展名保存,保存后,右键单击文件并选择编译脚本(x64),现在.exe文件已创建。
现在使用下面的代码:
os.system("C:\\Users\\mallikar\\Desktop\\screenUpload.exe")
【讨论】:
使用 pyautogui 可以非常简单地控制 Windows 文件选择器(或者只是您的操作系统)等组件。可以通过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')
【讨论】:
enter 选项未被识别。 pyautogui.write(filepath, interval=0.25) pyautogui.press('return')
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://www.example.org")
def upload_file():
upload_file = driver.find_element_by_id("paste here id of file which
is
enter code here`shown in html code...")
upload_file.send_keys("copy the path of file from your pc with name and
paste here...")
如果 toast 消息在几秒钟内消失,您可以使用 Chrome 中的 Chropath 扩展来查找其 xpath
这是一个纯python代码。
【讨论】:
不使用control_send,而是使用control_set_text 来解决发送到窗口的字符串中存在的不一致问题。请参阅此链接以供参考:https://www.autoitscript.com/forum/topic/85929-incorrect-string-being-sent-via-controlsend/
import autoit
autoit.win_wait_active("Open")
autoit.control_set_text("Open", "Edit1", imgPath)
autoit.send("{ENTER}")
【讨论】:
如果 sendkeys 功能对按钮不起作用,请使用 PyAutoGui。
示例代码:
import pyautogui
driver.find_element_by_xpath("Enter Xpath of File upload button").click()
time.sleep(4) #waiting for window popup to open
pyautogui.write(r"C:\Users\AmarKumar\FilesForUploading\image.jpg") #path of File
pyautogui.press('enter')
【讨论】:
【讨论】:
xpath的完整地址,您无法使用代码到达。@Ratery
service = Service('driver_path')
service.start()
driver = webdriver.Remote(service.service_url)
choose_image = driver.find_element(By.ID, 'id')
choose_image.send_keys(os.getcwd()+'/image.jpg')
selenium.common.exceptions.WebDriverException: Message: unknown command: unknown command: session/$sessionId/se/file
driver=webdriver.Chrome(executable_path=driver_path)
choose_image=driver.find_element(By.ID, 'id')
choose_image.send_keys(os.path.join(os.getcwd(), 'image.jpg'))
【讨论】: