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

11 个答案:

答案 0 :(得分:122)

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

drv.find_element_by_id("IdOfInputTypeFile").send_keys(os.getcwd()+"/image.png")

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

答案 1 :(得分: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

答案 2 :(得分:7)

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

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

您只需要安装AutoIt

https://pypi.python.org/pypi/PyAutoIt/0.3或只是" pip install -U pyautoit"通过cmd屏幕

输入" import autoit"在您的脚本页面上

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

autoit.win_active("开&#34)

autoit.control_send("开"" EDIT1" R" C:\用户\ UU \桌面\ TestUpload.txt&#34)

autoit.control_send("开"" EDIT1"" {ENTER}&#34)

醇>

它将查找打开文件对话框窗口并填写并按Enter键。

"开"是我的文件对话框屏幕的标题。把你的头衔代替"打开"。有更多创造性的方法来利用AutoIt的功能,但这对于初学者来说是一种简单,直接的方式。

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

autoit.control_set_text("Open","Edit1",r"C:\Users\uu\Desktop\TestUpload.txt")

答案 3 :(得分:5)

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

elm = driver.find_element_by_xpath("//input[@type='file']")

elm.send_keys(os.getcwd() + "/tests/sample_files/Figure1.tif")

在我的情况下,不需要提交表单或输入密钥。

感谢您的所有答案!它帮了很多忙!

答案 4 :(得分:4)

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

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

请参阅this question了解C#中的示例,我相信还有一种方法可以在Python中调用JavaScript,但我从未使用过Selenium Python绑定

答案 5 :(得分:2)

import win32com.client

shell = win32com.client.Dispatch("WScript.Shell")

shell.Sendkeys("C:\text.txt")

shell.Sendkeys("~")

将解决问题

答案 6 :(得分:0)

以下是我使用的代码:

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驱动程序。

答案 7 :(得分:0)

完整代码,可使用自动工具实现文件上传。

您只需复制粘贴此内容即可运行,因为它是实时演示,因此可以运行。

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()

下面是自动代码的内容:

WinWaitActive("File Upload")

Send("D:\SoftwareTestingMaterial\UploadFile.txt")

Send("{ENTER}")

下载autoIt和autoIt SCITE编辑器工具。

完成安装autoit并打开scite编辑器后,粘贴上面的代码并以.au3扩展名保存,保存后,右键单击该文件并选择complile脚本(x64),即创建了.exe文件。

现在使用以下代码:

os.system("C:\\Users\\mallikar\\Desktop\\screenUpload.exe")

答案 8 :(得分:0)

使用splinter:

browser.attach_file( 'file_chooser_id',fully_qualified_file_path)

答案 9 :(得分:0)

我使用以下脚本格式上传图片。这可能会对你有帮助。

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选择器。

答案 10 :(得分:0)

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

TOKEN="No, duh"

if [ ! -d "$HOME/Pictures/Screenshots" ]; then

mkdir -p ~/Pictures/Screenshots

fi

FILENAME="$(date +%s)"

screencapture -t png -i ~/Pictures/Screenshots/$FILENAME.png

file ~/Pictures/Screenshots/$FILENAME.png

JSON=$(curl -L -F image=@$HOME/Pictures/Screenshots/$FILENAME.png -H "Authorization: $TOKEN" -X POST https://mycooldomain.something/upload)

echo $JSON

URL=$(echo $JSON | jq --raw-output '.url');

echo $URL | pbcopy

osascript -e 'display notification "Upload successful" with title "I did a thing"'

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值