0

Essentially I am trying to automate/autofill a google form by using selenium and inspect element to find the class name. I am especially having trouble finding the solution to an "IndexError" problem when I try to input text into a "short answer" section of the google form. I am a beginner in Python so sorry if this may come across as a very low-level issue.

from selenium import webdriver

option = webdriver.ChromeOptions()
option.add_argument("-incognito")

browser = webdriver.Chrome(executable_path="path to selenium")

option = webdriver.ChromeOptions()
option.add_argument("-incognito")
email = "my email address to sign into the google form"

browser = webdriver.Chrome(executable_path="path to selenium", options=option)
browser.get('url of google form')

sign_in = browser.find_elements_by_class_name("whsOnd zHQkBf")
sign_in[0].send_keys(email)

Next = browser.find_elements_by_class_name("VfPpkd-RLmnJb")
Next[0].click()

textboxes = browser.find_elements_by_class_name("quantumWizTextinputPaperinputInput exportInput")
textboxes[0].send_keys("name")
    
radio_buttons = browser.find_elements_by_class_name("freebirdFormviewerComponentsQuestionCheckboxRoot")
radio_buttons[1].click()


submit=browser.find_element_by_class_name("appsMaterialWizButtonPaperbuttonLabel quantumWizButtonPaperbuttonLabel exportLabel")
submit.click()
BruceWayne
  • 22,923
  • 15
  • 65
  • 110
Mihir
  • 3
  • 3
  • You need to edit your question and add the relevant HTML. A link to the form would be nice also. We can't create locators for elements we can't see. – JeffC Apr 09 '21 at 05:37
  • When you use `find_element*_by_class_name()`, you can't have spaces in the class name. Those are actually two class names, e.g. your first locator "whsOnd zHQkBf". If you need to use both to be unique, turn it into a CSS selector, ".whsOnd.zHQkBf". If you are going to use the first element in the returned collection, you should just use `.find_element()` (singular) instead of `.find_elementS()` (plural). `.find_element()` will return the first element if more than one is a match. Looking at those class names, my guess is that they are randomly generated... that's probably why they don't work. – JeffC Apr 09 '21 at 05:40

1 Answers1

1

Are you sure sign_in actually has anything stored in it? It looks to me that those class names are auto generated when the page loads in, so it could that there is not actually a web element with the class name "whsOnd zHQkBf". It would be better to use xpath and use a relative path to your short form input field. This way if the general layout of the page changes you can still find your web element, making the solution more robust.

Updated:

Following code taken directly from Waits Selenium Python API Documentation and can be used to fix "NoSuchElement" exception or "element not interactable" in particular situations.

'''

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "myDynamicElement"))
    )
finally:
    driver.quit()

'''

You may use By.xpath or any other plethora of identifiers. If this code times out, then the element you are looking for does not exist.

dagarcia
  • 146
  • 5
  • I tried to use xpath and a relative path to see if it worked and for some reason it says it "uninteractable." I tried other relative paths but whenever they do work it still stops me at the list index out of range "IndexError" error. Is there a way to fix that? – Mihir Apr 09 '21 at 05:11
  • It looks like the array is empty. so you are right, I don't think anything was stored in it. – Mihir Apr 09 '21 at 05:19
  • I'm glad you the found your problem! As to your first comment, you may reach a "NoSuchElement" exception or "element not interactable" when the page has either 1.) not yet loaded the WebElement you are looking for or 2.) The element does not exist. To fix 1. try googling and learning a bit about the WebDriverWait function. Fixing 2 becomes difficult without having the actual form, but I believe your issue is 1. If you found my answer helpful please consider upvoting and accepting my answer! :) – dagarcia Apr 09 '21 at 18:41