使用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>
莫西·斯拉文(Moshe Slavin)

我会尝试使用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约定是对变量使用小写字母

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章