admin管理员组文章数量:1026134
I want to upload an image to Google Forms using Selenium.
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 open to any suggestions, whether it’s about tweaking the current approach or trying something entirely different.
import undetected_chromedriver as webdriver
from selenium.webdrivermon.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdrivermon.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("/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()
I want to upload an image to Google Forms using Selenium.
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 open to any suggestions, whether it’s about tweaking the current approach or trying something entirely different.
import undetected_chromedriver as webdriver
from selenium.webdrivermon.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdrivermon.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("/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()
本文标签: javascriptupload picture in google form seleniumStack Overflow
版权声明:本文标题:javascript - upload picture in google form selenium - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745635134a2160399.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论