简体   繁体   English

无法保存使用 Python Selenium 上传的文件

[英]Trouble saving file uploaded using Python Selenium

I am working in Selenium using Python and am working with Chrome.我正在使用 Python 在 Selenium 中工作,并且正在使用 Chrome。 When I get to a section for a picture upload, I do the following:当我进入图片上传部分时,我会执行以下操作:

    pictureChange = driver.find_element_by_xpath("//input[@class='custom-file' and @type='file']")
    photoLocation = [I enter the file location on my locally mapped drive]
    pictureChange.send_keys(photoLocation)

This seems to work as expected and the picture pops up in an overlay for cropping/zooming before saving the new picture.这似乎按预期工作,并且在保存新图片之前,图片会弹出用于裁剪/缩放的叠加层。 The overlay is a div class="modal-box" id="croppicModal."叠加层是 div class="modal-box" id="croppicModal"。 I am able to interact with the picture to zoom out and whatnot.我能够与图片进行交互以缩小和诸如此类。 But when I click "Save" (either manually or using my program), the new picture does not save.但是当我单击“保存”(手动或使用我的程序)时,新图片不会保存。 The overlay just goes away and the old picture is still showing.叠加层刚刚消失,旧图片仍在显示。 If I manually choose the file to upload and then click "Save," it works fine.如果我手动选择要上传的文件,然后单击“保存”,它可以正常工作。 It's just when I use the send_keys command to upload the photo that I then can't actually save it.只是当我使用 send_keys 命令上传照片时,我实际上无法保存它。 Any ideas why?任何想法为什么? Here is the Save button:这是保存按钮:

    <div class="action-btns"><span class="save-btn rounded-btn">Save</span><span class="croppic-cancel white-btn cancel-btn">Cancel</span></div>

If the file is still uploading through your send_keys strategy, I think the issue is not with the upload but with the method used to save the file.如果文件仍在通过您的send_keys策略上传,我认为问题不在于上传,而在于用于保存文件的方法。 I'm not sure what clicking strategy you are using, but you could try changing that up with some Javascript.我不确定您使用的是什么点击策略,但您可以尝试使用一些 Javascript 进行更改。

# locate save button
save_button = driver.find_element_by_xpath("//span[text()='Save']")

# click save button with JS
driver.execute_script("arguments[0].click();", save_button) 

If this does not work, we may be able to change the way you are uploading the file and see if that helps.如果这不起作用,我们可能会更改您上传文件的方式,看看是否有帮助。 But I am not convinced that the actual upload is the issue here.但我不相信实际上传是这里的问题。

I'd try using WebDriverWait :我会尝试使用WebDriverWait

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

wait = WebDriverWait(driver, 10)
picture_change = wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@class='custom-file' and @type='file']")))
photo_location = "Path/to/the/file"
picture_change.click()
picture_change.send_keys(photo_location)

save_button = wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Save']")))
save_button.click()

FYI: the python convention is to use lowercase for variables仅供参考:python 约定是对变量使用小写字母

you are trying to click on a div element which is not a button.您正在尝试单击不是按钮的 div 元素。 you need to find element with "button" tag that corresponds to a button you are trying to click您需要找到与您尝试单击的按钮相对应的带有“按钮”标签的元素

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

相关问题 使用Python请求库发送上传的文件时出现问题 - Trouble sending an uploaded file using the Python requests library 在python中使用Selenium在无头chrome中下载文件时文件未保存 - File Not Saving While Downloading File in Headless chrome using Selenium in python 使用 python 请求上传和上传文件而不保存 - Uploading and uploaded file without saving with python requests 我在将 Python (selenium) 输出保存到 txt 文件时遇到问题 - I'm having trouble saving my Python (selenium) output to a txt file 在python中使用硒登录到Uploaded.net - login to uploaded.net using selenium in python 无法将重复的 protobuf object 保存到文件(Python) - Trouble saving repeated protobuf object to file (Python) Python的问题:打开应用程序/保存文件 - Trouble with Python: Opening an app/saving a file 我无法将数据保存在变量中的 span 标签内,然后使用 selenium 和 python 以相同的形式将其输入到另一个字段中 - I'm having trouble saving the data inside a span tag in a variable and then entering it in another field in the same form using selenium and python Django / Python:在保存文件之前更改上传的文件名 - Django / Python : Change uploaded filename before saving file Flask / Python:保存前修改上传的文件数据 - Flask / Python: Modify uploaded file data before saving
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM