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.
Here’s what I’ve tried so far:
Locating the button – I've located the correct XPath for the "Add file" and file input button.
Waits and delays – I’ve added explicit waits (WebDriverWait) to ensure the elements are loaded before interacting with them.
JavaScript execution – I even attempted executing JavaScript directly to trigger the click event, but that didn’t solve the issue.
Other actions – I've tried switching to the frame or the dialog using switch_to.frame() just in case it’s an iframe, but that approach hasn’t helped either.
So far, I’ve been able to:
Open the Google Form.
Click the "Add file" button successfully.
Wait for the popup to appear. But I’m stuck at trying to upload the image itself.
I’m working on a project where this functionality is critical, and I really need to finish this as soon as possible. If anyone has experience with uploading images in Google Forms using Selenium, or any ideas on how I can fix this issue, please share your insights!
I’m open to any suggestions, whether it’s about tweaking the current approach or trying something entirely different. Thank you so much in advance for your help!
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
from selenium.webdriver.common.action_chains import ActionChains
import time
# Configuration des options de Chrome
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")
driver = webdriver.Chrome(options=options, use_subprocess=True)
try:
driver.get("https://forms.gle/HcKLXz9ErXkm63qK8")
print("Formulaire ouvert avec succès.")
# Cliquer sur le bouton 'Add file'
add_file_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '/html/body/div[1]/div[3]/form/div[2]/div/div[2]/div/div/div/div[2]/div/div[3]/span/span[2]'))
)
add_file_button.click()
print("Bouton 'Add file' cliqué avec succès.")
# Attendre que le popup apparaisse
popup = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '/html/body/div[1]/div[2]')) # XPath du popup
)
print("Popup visible.")
# Localiser le bouton 'Browser' à l'intérieur du popup
browser_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '/html/body/div[1]/div[2]//button[@aria-label="Browser"]')) # XPath du bouton 'Browser'
)
# Utiliser ActionChains pour s'assurer que le bouton est interactif
action = ActionChains(driver)
action.move_to_element(browser_button).click().perform()
print("Bouton 'Browser' cliqué avec succès.")
# Attendre que la boîte de dialogue de téléchargement soit ouverte
file_input = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//*[@type="file"]')) # XPath de l'élément de saisie de fichier
)
# Upload de l'image
file_path = "./01.png"
file_input.send_keys(file_path)
print(f"Image téléchargée avec succès : {file_path}")
# Soumettre le formulaire
submit_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '/html/body/div[1]/div[2]/div[3]/div[2]/div[2]/div/div/div/div[1]/div/div[2]/div/button'))
)
submit_button.click()
print("Formulaire soumis avec succès.")
except Exception as e:
print(f"Erreur: {e}")
finally:
driver.quit()
thanks you for your help