如何使用 Selenium 和 Python 在隐藏元素中上传图像
How to upload image within a hidden element using Selenium and Python
我试图在按钮内上传图片,但我一直收到此错误:
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")
要上传图片以在网站内进行转换 https://easypdf.com/fr/convertir-ocr using 您需要:
- 找到您必须调用
send_keys()
的 <input>
标记
- 将
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_07_2019.png')
浏览器快照:
更新
上传 图片 文件作为网页 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>
因此,当您尝试点击转换按钮时,您将面临recaptcha如下:
代码块:
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_07_2019.png')
driver.find_element_by_xpath("//button[@id='btnUpload']").click()
浏览器快照:
我试图在按钮内上传图片,但我一直收到此错误:
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")
要上传图片以在网站内进行转换 https://easypdf.com/fr/convertir-ocr using
- 找到您必须调用
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_07_2019.png')
浏览器快照:
<input>
标记
更新
上传 图片 文件作为网页 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>
因此,当您尝试点击转换按钮时,您将面临recaptcha如下:
代码块:
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_07_2019.png') driver.find_element_by_xpath("//button[@id='btnUpload']").click()
浏览器快照: