![](/img/trans.png)
[英]Upload profile picture on Microsoft with selenium, python ()
[英]How to upload file ( picture ) with selenium, python
selenium测试工具如何在web应用上传图片? 我正在使用 python。
我尝试了很多东西,但没有任何效果。
我正在做的是这个(确保 drv 是 webdriver 的一个实例):
drv.find_element_by_id("IdOfInputTypeFile").send_keys(os.getcwd()+"/image.png")
然后找到您的提交按钮并单击它。
控制诸如 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')
我为任何希望使用处理烦人的 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","{进入}”)
它将查找打开的文件对话框窗口并填写并按回车键。 “打开”是我的文件对话框屏幕的标题。 将您的标题替换为“打开”。 有更多创造性的方式来利用 AutoIt 的功能,但这对初学者来说是一种简单、直接的方式。
编辑:不要。 如果可以避免,请不要在大多数事情上使用 control_send。 它有一个众所周知的发送错误文本的问题。 就我而言,文件路径中的冒号变成了分号。 如果您需要发送输入键,应该没问题,但是如果您需要发送文本,请使用 control_set_text。 它具有相同的语法。
autoit.control_set_text("Open","Edit1",r"C:\Users\uu\Desktop\TestUpload.txt")
所有这些方法都不适用于 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
我正在使用Fine-uploader ,使用pytest
运行 selenium 测试,这对我pytest
:
elm = driver.find_element_by_xpath("//input[@type='file']")
elm.send_keys(os.getcwd() + "/tests/sample_files/Figure1.tif")
在我的情况下不需要表单提交或 Enter 键。
上传输入控件会打开一个本机对话框(由浏览器完成),因此通过 Selenium 单击控件或浏览按钮将弹出对话框并且测试将挂起。
解决方法是通过 JavaScript 设置上传输入的值(在 Java 中它是通过 JavascriptExecutor 完成的),然后提交表单。
在 C# 中查看此问题的示例,我确定还有一种方法可以在 Python 中调用 JavaScript,但我从未使用过 Selenium Python 绑定
import win32com.client
shell = win32com.client.Dispatch("WScript.Shell")
shell.Sendkeys("C:\text.txt")
shell.Sendkeys("~")
将解决问题
使用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")
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')
我使用以下脚本格式上传图像。 这可能对你有帮助。
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 选择器。
使用碎片:
browser.attach_file('file_chooser_id',fully_qualified_file_path)
这是我使用的代码:
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 驱动程序。
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'))
我发布了一个我认为最好的解决方案。 这是@sherlock 对解决方案的修改(请参阅我对该帖子的评论)
import autoit
autoit.win_wait_active("Open")
autoit.control_set_text("Open", "Edit1", imgPath)
autoit.control_click("Open", "Button1")
安装这三个
sudo apt-get 安装 python3-tk python3-dev
pip 安装传统知识
pip 安装 PyAutoGUI
使用这两行代码。
pyautogui.write('/home/image.jpg') pyautogui.press('enter')
from selenium import webdriver
import os
from selenium.webdriver.common.by import By
import time
import pyautogui
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
import autoit
import subprocess
driver = webdriver.Chrome()
driver.get("https://www.imagetotext.io/")
time.sleep(5)
element_present = EC.presence_of_element_located((By.XPATH, "//span[contains(text(),'Browse')]"))
WebDriverWait(driver, 5).until(element_present).click() # This opens the windows file selector
# Activate the file dialog box using AutoIt
autoit.win_activate("Open")
# Call the AutoIt script to handle the file upload dialog
# Path to the AutoIt compiled EXE file
autoit_exe_path = 'script.exe'
# Run the AutoIt script
subprocess.call(autoit_exe_path)
time.sleep(100)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.