# Input Form Submitting Problems with Selenium

**URL:** <https://discuss.python.org/t/input-form-submitting-problems-with-selenium/58492>\
**Category:** Python Help\
**Tags:** help\
**Created:** [July 17, 2024, 7:56pm UTC](https://discuss.python.org/t/input-form-submitting-problems-with-selenium/58492 "2024-07-17T19:56:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSG425](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ssg425/32/21168_2.png) [@SSG425](https://discuss.python.org/u/SSG425)\
**Post date:** [July 17, 2024, 7:56pm UTC](https://discuss.python.org/t/input-form-submitting-problems-with-selenium/58492/1 "2024-07-17T19:56:06Z")

</div>

Hello, for a freelancing project I’m working on scraping data from HVAC websites, and this [website here](https://www.goodmanmfg.com/support/find-a-dealer) is giving me trouble.  
What I want my code to do is take each zipcode in an array, enter it, submit, and scrape data like dealer names and addresses for each.  
My problem is that I don’t get past the submit step. When webdriver is not run in headless mode, I see the zipcode entered. Yet the page gives a “Enter valid zip/postal code” error

 ![Form_WebsiteExample](https://us1.discourse-cdn.com/flex002/uploads/python1/original/3X/1/7/17d21932485656c14d723fcbd677cbd55d05f890.png)

Here is the relevant code:

````python
```python
# Import required modules
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
from selenium.webdriver.support.ui import Select
import codecs
import re
from time import sleep
#importing zipcode array
from array import array
from zipcode_list import zip_arr
#importing csv
import csv
#time to wait for element to appear 
timeout = 20

#checking that dealers have been found
def is_multiple_dealers(driver):
        dealer = driver.find_elements(By.XPATH, "//div[contains(@class, 'dl-dealer-count__block')]/span")
        for d in dealer:
                if '0 dealers near' in d.text:
                        return False, d.text
                        break
                else:
                        return True, d.text
                        break
                break 

def main():

        ffOptions = webdriver.FirefoxOptions()
        ffOptions.set_preference("geo.enabled", False)
        ffOptions.set_preference('geo.prompt.testing', False)
        ffOptions.set_preference('geo.prompt.testing.allow', False)
        driver = webdriver.Firefox(options=ffOptions)
        wait = WebDriverWait(driver, timeout)
        a = zip_arr
        zipcode_input = a[0]
        
        #Redirect
        val = 'https://www.goodmanmfg.com/support/find-a-dealer'
        driver.get(val)
        wait.until(EC.url_to_be(val))
        page_source = driver.page_source
        sleep(1)
        driver.refresh()

        #setting up arrays for csv
        zipcode_status_array = []
        names_array = []
        address_array = []
        phone_array = []
        
        for x in range(len(a)):    
                zipcode_input = a[x]
                #waiting until input box is there then typing zipcode
                zipcode_form = wait.until(EC.element_to_be_clickable((By.ID, 'zipcode')))
                zipcode_form.click()
                zipcode_form.clear()
                driver.execute_script("arguments[0].value = '" + str(zipcode_input) + "';", zipcode_form) 

                sleep(2)
                #selecting 10 in the "Miles From Home" dropdown
                select = Select(driver.find_element(By.ID, 'miles'))
                select.select_by_value('10')
                
                #waiting until submit button is there then submitting
                sleep(2)
                submit_zipcode = wait.until(EC.presence_of_element_located((By.XPATH, "//button[contains(., 'SEARCH')]")))
                driver.execute_script("arguments[0].click();", submit_zipcode) 

                wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div.dl-dealer-details')))
                dealer_boolean, dealer_quantity_text = is_multiple_dealers(driver)
                if dealer_boolean == True:
                        print(dealer_quantity_text)
                        print("")
                        dealer_items = driver.find_elements(By.CSS_SELECTOR, 'div.dl-dealer-details')
                        for dealer in dealer_items:
                                #code for finding elements on dealers page and appending to respective arrays
                                print("")
                        print("")
                else:
                        #append "NAN" to relevant arrays for entry
                        print("")
                        print("")
                sleep(1)
                
        #code here for writing arrays to csv, not relevant

        print("complete")
        driver.quit()

if __name__ == " __main__":
        main()

````

I first tried using sendkeys(), then currently javascript, but neither were recognized by the website.  
My current traceback is a TimeoutException for

```python
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div.dl-dealer-details')))

```

which again just supports that my zipcode inputting code is running, it’s just that the website isn’t accepting it and proceeding?  
This is my first post here, but I’m just really hoping I can get someone more knowledgeable to finally solve this, since the alternative has been hours of entering data manually. Anything would be appreciated!

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [July 18, 2024, 7:42am UTC](https://discuss.python.org/t/input-form-submitting-problems-with-selenium/58492/2 "2024-07-18T07:42:57Z")

</div>

I expect you will need to debug the javascript that is reporting the error.

In the past I have found work arounds like inserting the value directly into a variable the javascript uses.

---

<div class="post-metadata">

**Author:** ![SSG425](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ssg425/32/21168_2.png) [@SSG425](https://discuss.python.org/u/SSG425)\
**Post date:** [July 18, 2024, 5:54pm UTC](https://discuss.python.org/t/input-form-submitting-problems-with-selenium/58492/3 "2024-07-18T17:54:45Z")

</div>

Thanks for the response.

I know this is for Python, not Javascript, but the search function seemed like an obvious place to start:

```js
//ajax call function for search button
    function Search() {
        var zipcode = $("#zipcode").val();
        var miles = $("#miles option:selected").text();
        var featuresSelected = [];

        if (zipcode == "") {
            $("#errMsg").empty();
            $("#errMsg").text(" Please enter your zip or postal code");
        }

        else if (miles.toLowerCase() == "miles from home") {
            $("#errMsg").empty();
            $("#errMsg").text("Please select your search range by selecting the number of miles from your home");
        }

        else if ($('input[name=features]:checked').length == 0) {
            $("#errMsg").empty();
            $("#errMsg").text("Please select one or more of the applicable filters");
        }

        else {
            var loc = navigator.webdriver;
            if (validateZip() && (loc === undefined || loc === false)) {
                validate().then(function (data) {

                    $('input[name=features]:checked').each(function () {
                        featuresSelected.push($(this).attr("value"));
                    });
                    $('#preLoader').show();
                    grecaptcha.ready(function () {
                        grecaptcha.execute('6LfViS4pAAAAAEWhMM-QiYK-dqKZHenujXBkIg9R', { action: 'submit' }).then(function (token) {
                            let googleCaptchaToken;
                            googleCaptchaToken = token;

                            if (googleCaptchaToken) {
                                $.ajax({
                                    url: "/json/DealerLocatorGoodman/search",
                                    type: 'GET',
                                    traditional: true,
                                    data: { zipcode: zipcode, miles: miles, features: featuresSelected, token: googleCaptchaToken },
                                    //dataType: "json",
                                    success: function (data) {
                                        //$('#content_' + modelnumber + "-" + unique).html(data);
                                        $("#errMsg").empty();
                                        $("#dealerResults").empty();
                                        $("#dealerResults").append(data);
                                        var sZip = $('#zipcode').val();
                                        $("#searchedZip").text(sZip);
                                        $('#preLoader').hide();
                                        //fixHeight("list-column");
                                        fixHeight("dl-store-name");
                                        fixHeight("dl-store-address");
                                        fixHeight("dl-store-icons");
                                        (function ($) {
                                            $('#dealer-list-pagination').simplePagination({
                                                items_per_page: 8
                                            });

                                        })(jQuery);
                                    }

                                });
                            }
                        });
                    });
                    });

                    //alert(mfgtext);

            }
            else {
                $("#errMsg").text("Enter valid zip/postal code");
            }

        }
    }

```

.val() is jquery related, so after searching I thought I’d first import requests and do something like this to load jquery:

```python
//this is between the redirect and array setup parts
jquery_url = 'https://code.jquery.com/jquery-3.5.1.min.js'
r = requests.get(jquery_url)
jquery_js = r.text
driver.execute_script(jquery_js)

```

Then this in the for loop right after the waiting and clicking on the zipcode id

```python
driver.execute_script("arguments[0].val('" + str(zipcode_input) +"');", zipcode_form)

```

Which just gave:

```python
selenium.common.exceptions.JavascriptException: Message: TypeError: arguments[0].val is not a function
Stacktrace:
@https://www.goodmanmfg.com/support/find-a-dealer:2:20
@https://www.goodmanmfg.com/support/find-a-dealer:3:8

```

I’m not sure how to get execute\_script to recognize the function then…
