【问题标题】:How to upload image within a hidden element using Selenium and Python如何使用 Selenium 和 Python 在隐藏元素中上传图像
【发布时间】:2020-05-19 05:54:39
【问题描述】:

我正在尝试上传按钮内的图片,但我不断收到此错误:

selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
(Session info: chrome=79.0.3945.130)

这是我的代码

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait

import time
import os

driver = webdriver.Chrome()
driver.get("https://easypdf.com/fr/convertir-ocr")
driver.maximize_window()

time.sleep(10)
driver.find_element_by_xpath('//*[@id="social"]/div/div[1]').click()

uploadPhotoBtn = driver.find_element_by_xpath('//*[@id="dzupload"]/div')
driver.execute_script('arguments[0].style = ""; arguments[0].style.display = "block"; arguments[0].style.visibility = "visible";', uploadPhotoBtn)
uploadPhotoBtn.send_keys("C:\\Users\\basma\\Desktop\\python\\toImg\\jpg0.jpg")

【问题讨论】:

    标签: python selenium image-uploading hidden-field invisible-recaptcha


    【解决方案1】:

    要使用Selenium 在网站https://easypdf.com/fr/convertir-ocr 内上传图片以进行转换,您需要:

    • 找到<input> 标记,您必须在其中调用send_keys()
    • type属性的值从hidden更改为text
    • 调用send_keys()
    • 代码块:

      from selenium import webdriver
      
      options = webdriver.ChromeOptions() 
      options.add_argument("start-maximized")
      options.add_experimental_option("excludeSwitches", ["enable-automation"])
      options.add_experimental_option('useAutomationExtension', False)
      driver = webdriver.Chrome(options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
      driver.get("https://easypdf.com/fr/convertir-ocr")
      element = driver.find_element_by_xpath("//input[@id='tool-name']")
      driver.execute_script("document.getElementById('tool-name').setAttribute('type','text')")
      element.click()
      element.clear()
      element.send_keys(r'C:\Users\Debanjan.B\Desktop\screenshots\31_07_2019.png')
      
    • 浏览器快照:


    更新

    由于网页https://easypdf.com/fr/convertir-ocrInvisible reCAPTCHA 保护,因此上传图像 文件以进行进一步处理会有点困难。

    <div class="rc-anchor rc-anchor-invisible rc-anchor-light  rc-anchor-invisible-hover"><div id="recaptcha-accessible-status" class="rc-anchor-aria-status" aria-hidden="true">Veuillez valider le test reCAPTCHA.. </div><div class="rc-anchor-error-msg-container" style="display:none"><span class="rc-anchor-error-msg" aria-hidden="true"></span></div><div class="rc-anchor-normal-footer smalltext" aria-hidden="true"><div class="rc-anchor-logo-large" role="presentation"><div class="rc-anchor-logo-img rc-anchor-logo-img-large"></div></div><div class="rc-anchor-pt"><a href="https://www.google.com/intl/fr/policies/privacy/" target="_blank">Confidentialité</a><span aria-hidden="true" role="presentation"> - </span><a href="https://www.google.com/intl/fr/policies/terms/" target="_blank">Conditions</a></div></div><div class="rc-anchor-invisible-text"><span>protection par <strong>reCAPTCHA</strong></span><div class="rc-anchor-pt"><a href="https://www.google.com/intl/fr/policies/privacy/" target="_blank">Confidentialité</a><span aria-hidden="true" role="presentation"> - </span><a href="https://www.google.com/intl/fr/policies/terms/" target="_blank">Conditions</a></div></div></div>
    

    因此,当您尝试点击转换按钮时,您将面临,如下所示:

    • 代码块:

      driver.get("https://easypdf.com/fr/convertir-ocr")
      element = driver.find_element_by_xpath("//input[@id='tool-name']")
      driver.execute_script("document.getElementById('tool-name').setAttribute('type','text')")
      element.click()
      element.clear()
      element.send_keys(r'C:\Users\Debanjan.B\Desktop\screenshots\31_07_2019.png')
      driver.find_element_by_xpath("//button[@id='btnUpload']").click()
      
    • 浏览器快照:

    【讨论】:

    • 感谢@DebanjanB 的回答,它可以正常工作,但在这种情况下,我们只上传文本,我需要上传图片才能进行转换,您能帮我解决这个问题吗?跨度>
    • @Lara 粗略的 Stackoverflow 贡献者在这里为您提供帮助。请随时针对您的新要求提出新问题。
    • 还是不能上传图片,只能上传图片的名字,在尝试toc转换的时候,说没有选择文件,不知道我该如何解决这个问题,请帮助我
    • @Lara 你有没有看到任何错误?您能否针对您面临的错误提出一个新问题?
    • @Lara 检查答案更新并让我知道状态
    猜你喜欢
    • 2020-01-27
    • 2019-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-08
    • 1970-01-01
    相关资源
    最近更新 更多