[英]Trouble saving file uploaded using Python Selenium
我正在使用 Python 在 Selenium 中工作,并且正在使用 Chrome。 当我进入图片上传部分时,我会执行以下操作:
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)
这似乎按预期工作,并且在保存新图片之前,图片会弹出用于裁剪/缩放的叠加层。 叠加层是 div class="modal-box" id="croppicModal"。 我能够与图片进行交互以缩小和诸如此类。 但是当我单击“保存”(手动或使用我的程序)时,新图片不会保存。 叠加层刚刚消失,旧图片仍在显示。 如果我手动选择要上传的文件,然后单击“保存”,它可以正常工作。 只是当我使用 send_keys 命令上传照片时,我实际上无法保存它。 任何想法为什么? 这是保存按钮:
<div class="action-btns"><span class="save-btn rounded-btn">Save</span><span class="croppic-cancel white-btn cancel-btn">Cancel</span></div>
如果文件仍在通过您的send_keys
策略上传,我认为问题不在于上传,而在于用于保存文件的方法。 我不确定您使用的是什么点击策略,但您可以尝试使用一些 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)
如果这不起作用,我们可能会更改您上传文件的方式,看看是否有帮助。 但我不相信实际上传是这里的问题。
我会尝试使用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()
仅供参考:python 约定是对变量使用小写字母
您正在尝试单击不是按钮的 div 元素。 您需要找到与您尝试单击的按钮相对应的带有“按钮”标签的元素
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.