Search code examples
pythonpython-3.xseleniuminputgetattribute

Getting text data with .getAttribute in Selenium


I'm trying to retrieve the text from a container/card text box on this Google form. I basically want to retrieve the questions and related answers.

I can retrieve the questions using css code but I don't know how to get the answers. The answers are included, more or less, in this type of code:

<input type="text" class="quantumWizTextinputSimpleinputInput exportInput" jsname="YPqjbf" autocomplete="on" tabindex="0" aria-label="valeur d'une option" value="Doctor's availability in hotel" dir="ltr" data-initial-value="Doctor's availability in hotel">

Here the answer is:

data-initial-value="Doctor's availability in hotel"

Here we want to get:

data-initial-value="Doctor's availability in hotel"

So, I tried the next one with `.get_attribute':

# I get all the letters with the questions and answers inside
containers = driver.find_elements_by_class_name(
    "freebirdFormeditorViewItemContentWrapper"
)
questionnaire = {}
# for each card
for container in containers:
    try:
        question = container.find_element_by_css_selector(".exportTextarea[aria-label='Intitulé de la question']")
    except NoSuchElementException:
        print("NoSuchElementException in " + container)
        continue
    # Get the answers
    responses = container.find_elements_by_class_name(
        "quantumWizTextinputSimpleinputInput.exportInput"
    )
    print("responses: ", responses)
    extracted_responses = [response.get_attribute("data-initial-value") for response in responses]

    questionnaire[question.text] = extracted_responses 

But I don't get the answers:

responses:  []

Solution

  • Try:

    responses = container.find_element_by_css_selector(
      ".quantumWizTextinputSimpleinputInput.exportInput"
    )