简体   繁体   English

如何使用 Selenium 和 Python 在隐藏元素中上传图像

[英]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")

To upload image for conversion within the website https://easypdf.com/fr/convertir-ocr using Selenium you need to:要使用Selenium在网站https://easypdf.com/fr/convertir-ocr 中上传图像以进行转换,您需要:

  • Locate the <input> tag where you have to invoke send_keys()找到必须调用send_keys()<input>标记
  • Change the value of type attribute from hidden to texttype属性的值从hidden更改为text
  • Invoke send_keys()调用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:浏览器快照:

图片上传


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 .由于网页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>

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM