Hello everyone,
I’m reaching out for some help regarding uploading an image to Google Forms using Selenium. I’ve been working on this for days, and despite trying various approaches, I still can't make it work. Here’s the situation:
I’ve managed to open the form and even triggered the file upload dialog box by clicking the “Add file” button. However, when it comes to interacting with the dialog, specifically clicking on the file input button (type="file"), I keep hitting a wall. The button is present in the DOM and visible on the page, but Selenium doesn’t seem to be able to interact with it.
My code :
import undetected_chromedriver as webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import os
import time
# Configuration des options de Chrome pour se connecter au Profile 1
options = webdriver.ChromeOptions()
profile = r"C:\\Users\\User\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 1"
options.add_argument(f"user-data-dir={profile}")
options.add_argument("profile-directory=Profile 1")
# Initialisation du driver avec les options configurées
driver = webdriver.Chrome(options=options, use_subprocess=True)
try:
# URL du formulaire Google
driver.get(form_url)
print("Formulaire Google ouvert avec succès.")
# Étape 2 : Localiser et utiliser directement le champ <input type='file'>
print("Étape 2 : Localisation du champ <input type='file'> et téléchargement direct")
file_input = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/div[2]/div[3]/div[2]/div[2]/div/div/div/div[1]/div/div[2]/div/button/div[3]'))
)
file_path = os.path.abspath("./01.png") # Chemin absolu vers l'image
print(f"Téléchargement de l'image : {file_path}")
file_input.send_keys(file_path) # Charger l'image dans le champ
print("Image téléchargée avec succès.")
# Étape 4 : Simulation de l'événement 'input' pour informer le formulaire
print("Étape 4 : Simulation de l'événement 'input'")
driver.execute_script(
"arguments[0].dispatchEvent(new Event('input', { bubbles: true }));", file_input
)
print("Événement 'input' déclenché avec succès.")
# Étape 5 : Cliquer sur le bouton "Submit" pour soumettre le formulaire
print("Étape 5 : Localisation et clic sur le bouton 'Submit'")
submit_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//span[text()="Submit"]/parent::button'))
)
submit_button.click()
print("Formulaire soumis avec succès.")
except Exception as e:
print(f"Erreur : {e}")
finally:
# Attendre avant de fermer le navigateur
time.sleep(5)
driver.quit()
print("Navigateur fermé.")
My error message :
PS C:\Users\User> & C:/Python/Python313/python.exe c:/Users/User/Documents/MySeleniumProject/Revisions/TestUpload.py
Formulaire Google ouvert avec succès.
Étape 2 : Localisation du champ <input type='file'> et téléchargement direct
Erreur : Message:
Stacktrace:
GetHandleVerifier [0x010D5093+25075]
(No symbol) [0x0105E124]
(No symbol) [0x00F3BE63]
(No symbol) [0x00F7FD06]
(No symbol) [0x00F7FF4B]
(No symbol) [0x00FBD8C2]
(No symbol) [0x00FA1EC4]
(No symbol) [0x00FBB48E]
(No symbol) [0x00FA1C16]
(No symbol) [0x00F73F3C]
(No symbol) [0x00F74ECD]
GetHandleVerifier [0x013C2523+3094147]
GetHandleVerifier [0x013D5754+3172532]
GetHandleVerifier [0x013CDF32+3141778]
GetHandleVerifier [0x01172100+668256]
(No symbol) [0x01066C4D]
(No symbol) [0x01063DF8]
(No symbol) [0x01063F95]
(No symbol) [0x01056C80]
BaseThreadInitThunk [0x75FE7BA9+25]
RtlInitializeExceptionChain [0x773EC0CB+107]
RtlClearBits [0x773EC04F+191]
Navigateur fermé.
Please tell me what can I do ?
Thanks you