[英]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.