【问题标题】:How do I upload an image from another directory of my project on web site with Selenium - Python?如何使用 Selenium - Python 从网站上我的项目的另一个目录上传图像?
【发布时间】:2023-04-02 18:09:01
【问题描述】:

我需要在网站上上传图片,我想将图片存储在我的项目中的单独目录“image”中,这样​​每个人都可以运行我的测试并上传图片而不会出现路径问题。

这是上传表单的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">

关于上传表单:网站没有输入框,只能通过系统窗口选择文件。

这是我的代码:

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

显然,不起作用。还阅读了有关与“输入文件”交互的信息,但不知道如何应用。

有什么想法吗?
提前致谢!

【问题讨论】:

标签:
python
selenium-webdriver