简体   繁体   English

如何使用Selenium-Python从网站上项目的另一个目录上载图像?

[英]How do I upload an image from another directory of my project on web site with Selenium - Python?

I need to upload an image on web site, and I wanna store the image in my project in a separate directory "image", so everyone can run my test and upload image with no path issues. 我需要在网站上上传图像,并且要将图像存储在项目中的单独目录“ image”中,以便每个人都可以运行我的测试并上传图像而没有路径问题。

here's html code of uploading form: 这是上传表单的html代码:

<form id="imageUploadForm" class="ant-form ant-form-horizontal image-upload-form">
<div class="ant-row ant-form-item">
<div class="ant-col ant-form-item-label ant-col-xs-24 ant-col-sm-4">
<label class="" title="Image list">Image list</label>
</div>
<div class="ant-col ant-form-item-control-wrapper ant-col-xs-24 ant-col-sm-16">
<div class="ant-form-item-control">
<span class="ant-form-item-children">
<div class="images-list">
<div class="images-list__upload-btn">
<span class="">
<div class="ant-upload ant-upload-select ant-upload-select-text">
<span tabindex="0" class="ant-upload" role="button">
<input id="image" type="file" accept="" style="display: none;">
<button type="button" class="ant-btn image-upload__btn">
<i aria-label="icon: plus" class="anticon anticon-plus">
<svg viewBox="64 64 896 896" class="" data-icon="plus" width="1em" height="1em" fill="currentColor" aria-hidden="true" focusable="false">

as for uploading form : there's no input field on a site, u can choose a file only via a system window. 至于上载表格:网站上没有输入字段,您只能通过系统窗口选择文件。

here's my code: 这是我的代码:

import os

image_upload = wd.find_element_by_xpath("//*[@id='imageUploadForm']/div[1]/div[2]/div/span/div/div[1]/span/div")

// tap on a button which opens a system window
 image_upload.click()

//trying to send path to a file which stored in my project
image_upload.send_keys(os.getcwd().replace("fixture", "") + "images/variant_1.png")

obviously, does not work. 显然,不起作用。 Also read smth about interaction with "input file", but didn't get how to apply. 还阅读了有关与“输入文件”交互的信息,但未了解如何应用。

Any ideas? 有任何想法吗? Thanks in advance! 提前致谢!

Hi from your snapshot there is an input tag with id="image". 您好,快照中有一个id为“ image”的输入标签。 try sending the path of your image you are willing to upload using sendtext and click on submit it will work. 请尝试使用sendtext发送您愿意上传的图像的路径,然后单击“提交”将起作用。

the code should be as follows: 代码应如下所示:

xpath ="//*[@id=\\"image\\"]; driver.FindElement(By.XPath(xpath)).SendKeys("Your image path");

then perform click event on the upload button and it will work 然后在上传按钮上执行点击事件,它将起作用

first there is an input element which you send key to instead of an form element. 首先,有一个输入元素,您将键发送给该输入元素,而不是表单元素。 Then try not click to input element because it will trigger click event and open os file detector which selenium could not handle. 然后尝试不要单击输入元素,因为它将触发单击事件并打开硒无法处理的os文件检测器。

image_upload_input = wd.find_element_by_xpath("//*[@id='image']")
image_upload.send_keys(os.getcwd().replace("fixture", "") + "images/variant_1.png")

If somehow send key still trigger opening os file detector, you need to override this by file_detector in selenium: 如果以某种方式发送密钥仍触发打开os文件检测器,则需要使用file_detector中的file_detector覆盖它:

from selenium.webdriver.remote.file_detector import UselessFileDetector
wd.file_detector = UselessFileDetector()

In nutshell, whole code will be: 简而言之,整个代码将是:

from selenium.webdriver.remote.file_detector import UselessFileDetector
wd.file_detector = UselessFileDetector()
image_upload_input = wd.find_element_by_xpath("//*[@id='image']")
image_upload.send_keys(os.getcwd().replace("fixture", "") + "images/variant_1.png")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在我的网站中修复此图片上传错误? 网站基于Python构建 - How do I fix this image upload error in my website? Site is built on Python 我是否需要将项目目录添加到每个脚本中的系统路径以从另一个目录导入函数? - Do I need to add my project directory to the system path in every script to import a function from another directory? 如何使用Selenium Python上传图像? - How to upload an image with selenium python? 对于Python 3,如何从另一个目录导入.py文件? - For Python 3, how do I import a .py file from another directory? 我想在网络项目中使用python。 如何在我的网络项目中使用python [保留] - I want to python in web projects. How do I use python in my web project [on hold] 我应该将我的Python项目添加到site-packages目录中,还是将项目附加到PYTHONPATH? - Should I add my Python project to the site-packages directory, or append my project to PYTHONPATH? 如何从pygame的python中的其他目录中打开图像 - How do I open an image from a different directory in python for pygame 如何使Python站点软件包在安装目录之外可用? - How do I make Python site packages available outside of my install directory? 如何找到我的 Python 站点包目录的位置? - How do I find the location of my Python site-packages directory? 如何从另一个目录访问图像? 蟒蛇 - How to Access a image from another directory? python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM