Search code examples
pythonseleniumimage-uploadinghidden-fieldinvisible-recaptcha

How to upload image within a hidden element using Selenium and Python


I'm trying to upload a picture inside a button, but I keep getting this error:

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

This is my code

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")

Solution

  • To upload image for conversion within the website https://easypdf.com/fr/convertir-ocr using Selenium you need to:

    • Locate the <input> tag where you have to invoke send_keys()
    • Change the value of type attribute from hidden to text
    • Invoke send_keys()
    • Code Block:

      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')
      
    • Browser Snapshot:

    image_upload


    Update

    It would be a bit tough to upload an image file for further processing as the webpage https://easypdf.com/fr/convertir-ocr is protected by Invisible 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>
    

    Hence, when you try to click the button for conversion, you will face the as follows:

    • Code Block:

      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()
      
    • Browser Snapshot:

    invisible_reCAPTCHA