3

I iterate on a google form survey and try to put some content (which I try to make look like a quote just in case). However some fields are ages and do not allow to be more than 99 years old like this:

<input type="text" class="quantumWizTextinputPaperinputInput exportInput" jsname="YPqjbf" autocomplete="off" tabindex="0" aria-label="Age" aria-describedby="i.desc.504994172 i.err.504994172" name="entry.128750970" value="" min="18" max="99" required="" dir="auto" data-initial-dir="auto" data-initial-value="10102015" badinput="false" aria-invalid="true">

introducir la descripción de la imagen aquí

So I added a condition in my code to try to see if there is a 'max' attribute on the elements I have to write on:

        content_areas = driver.find_elements_by_class_name(
            "quantumWizTextinputSimpleinputInput.exportInput"
        )
        for content_area in content_areas:
            if content_area.get_attribute("max") exists:
                max = content_area.get_attribute("max")
                content_area.send_keys(max)
            else:
                content_area.send_keys("10102015")

But it doesn't work:

max:
Traceback (most recent call last):
  File "questions_scraper_michael.py", line 151, in <module>
    result = extract(driver, df, column)
  File "questions_scraper_michael.py", line 70, in extract
    "freebirdFormviewerViewNumberedItemContainer"
  File "C:\Users\antoi\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\selenium\webdriver\remote\webdriver.py", line 580, in find_elements_
by_class_name
    return self.find_elements(by=By.CLASS_NAME, value=name)
  File "C:\Users\antoi\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\selenium\webdriver\remote\webdriver.py", line 1007, in find_elements

    'value': value})['value'] or []
  File "C:\Users\antoi\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\antoi\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\selenium\webdriver\remote\errorhandler.py", line 241, in check_respo
nse
    raise exception_class(message, screen, stacktrace, alert_text)
selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: {Alert text :
Message: unexpected alert open: {Alert text : }
  (Session info: chrome=83.0.4103.61)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Revolucion for Monica
  • 2,848
  • 8
  • 39
  • 78

4 Answers4

1

I think your solution should be like this

  content_areas = driver.find_elements_by_class_name(
            "quantumWizTextinputSimpleinputInput.exportInput"
        )
        for content_area in content_areas:
            if content_area.get_attribute("max") and not content_area.get_attribute("max").isspace():
                max = content_area.get_attribute("max")
            else:
                content_area.send_keys("10102015")
Norayr Sargsyan
  • 1,737
  • 1
  • 12
  • 26
  • Thanks for the answer, I guess the double dot `content_area..` is a typo? Unfortunately even without that it doesn't allow me to get this attribute yet. I've provided a full link to the webpage I'm scraping from. It's a google form. – Revolucion for Monica Jun 04 '20 at 16:39
1

Try below css selector to identify all input elements on that page and then iterate the loop.

driver.get('https://docs.google.com/forms/d/e/1FAIpQLSe-ebOztdB6T4ZgtsOYuvbUR5qwSTfI5CnJB1mNLeNflCVX8Q/viewform')
content_areas=driver.find_elements_by_css_selector("input.exportInput")
for content_area in content_areas:
    if content_area.get_attribute("max"):
        max = content_area.get_attribute("max")
        content_area.send_keys(max)
    else:
        content_area.send_keys("10102015")

Browser Snapshot.

enter image description here

KunduK
  • 32,888
  • 5
  • 17
  • 41
0

There is no "exists" command in python. You should remove it.

for content_area in content_areas:
    if content_area.get_attribute("max"):
        max = content_area.get_attribute("max")
    else:
        content_area.send_keys("10102015")
Metalgear
  • 3,391
  • 1
  • 7
  • 16
  • Thanks for noticing, I just wrote it to mention that it had to be accessible. Unfortunately I wasn't able to get it with your help yet. I am going to provide the url to the webpage which is a google forms survey – Revolucion for Monica Jun 04 '20 at 16:31
0

To summarize, your test involves:

  • Check the presence of max attribute.
  • If such elements are present, then retrieve the value of max attribute.

Ideally, the max attribute specifies the maximum value for an <input> element. So you need to induce WebDriverWait for the visibility_of_all_elements_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    driver.get('https://docs.google.com/forms/d/e/1FAIpQLSe-ebOztdB6T4ZgtsOYuvbUR5qwSTfI5CnJB1mNLeNflCVX8Q/viewform')
    print([my_elem.get_attribute("max") for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "input.quantumWizTextinputPaperinputInput.exportInput[max]")))])
    
  • Using XPATH:

    driver.get('https://docs.google.com/forms/d/e/1FAIpQLSe-ebOztdB6T4ZgtsOYuvbUR5qwSTfI5CnJB1mNLeNflCVX8Q/viewform')
    print([my_elem.get_attribute("max") for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.XPATH, "//input[@class='quantumWizTextinputPaperinputInput exportInput' and @max]")))])
    
  • Console Output:

    ['99']
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352