10.Python Selenium Guide - Submitting Forms with Selenium _ ScrapeOps
10.Python Selenium Guide - Submitting Forms with Selenium _ ScrapeOps
Selenium stands out as a powerful and versatile tool, providing a seamless solution for automating
browser actions. With Selenium, we can find any element on a page and we have the power to interact
with it just like a regular human user would. Not only can we find these elements quickly, but we can fill
and submit them at lightning speed in comparison to a regular user.
In this article, we will delve into the fundamental concepts, practical examples, and best practices for
harnessing the full potential of Selenium to navigate and interact with web forms.
TLDR
Interacting With Form Elements
Methods of Submitting Forms
Uploading Files with Selenium
Troubleshooting
Conclusion
More Web Scraping Guides
https://scrapeops.io/selenium-web-scraping-playbook/python-selenium-submit-form/ 1/20
3/31/24, 9:31 PM Python Selenium Guide - Submitting Forms with Selenium | ScrapeOps
1. Locate the Form: Identify the HTML form on the webpage using Selenium's element locating
methods.
2. Fill in Data: Use Selenium to input data into the form fields, mimicking user interactions.
3. Submit the Form: Locate the submit button or trigger the form submission action using Selenium.
The example below shows a basic breakdown of how to fill a form in Selenium:
https://scrapeops.io/selenium-web-scraping-playbook/python-selenium-submit-form/ 2/20
3/31/24, 9:31 PM Python Selenium Guide - Submitting Forms with Selenium | ScrapeOps
2. find_elements() finds all elements of a certain criteria and returns them in the form of a list.
As we have done already, we can select values by saving them as variables. Anytime you use
find_element() or find_elements() , you get a web element that you can try all sorts of various
methods to interact with. When filling an input box, we use the send_keys() method.
In the TLDR section above, we used find_element() to find each individual input field. Let's revise the
example to find both elements and return them in a list.
Deleting values in input boxes is often necessary for several reasons in web scraping, particularly to
clear existing search queries or filter criteria, enabling the scraper to input new parameters and
dynamically adapt to changing requirements, ultimately retrieving the desired data accurately.
If we wish to delete values from an input box, we can use the clear() method. This method removes all
input from our web element. Take a look at the example below:
https://scrapeops.io/selenium-web-scraping-playbook/python-selenium-submit-form/ 3/20
3/31/24, 9:31 PM Python Selenium Guide - Submitting Forms with Selenium | ScrapeOps
Checkboxes are commonly used in forms and settings to allow users to select or deselect options.
When selecting checkboxes, we simply click() them just like you would when browsing the web like
normal. The click() method gives us the power to do anything we would normally do with our mouse,
and to think the way we would think when browsing the web.
The example below finds all elements, of a certain type and selects them.
if not element.is_selected():
element.click()
sleep(1)
else:
None
#close the browser
driver.quit()
Radio buttons are commonly used in forms to allow users to choose a single option from a set.
When interacting with radio buttons, we also use the click() method. Not all the elements returned
are clickable, so we use a try statement to attempt clicking them and an except statement to do
nothing with them if they're not clickable.
https://scrapeops.io/selenium-web-scraping-playbook/python-selenium-submit-form/ 5/20
3/31/24, 9:31 PM Python Selenium Guide - Submitting Forms with Selenium | ScrapeOps
In the example above, we followed a similar route with the previous example.
Located all elements on the page with the class name "usa-radio__label" and stored them in the
elements list.
Iterated through each element in the elements list.
Checked if the radio button was not already selected using element.is_selected() .
If not selected, attempted to simulate a click on the radio button using element.click() and
introduced a 1-second delay using sleep(1) for visualization. If an exception occurred during
the click attempt, did nothing ( except: None ).
If the radio button was already selected, did nothing ( else: None ).
Quit the Chrome browser instance using driver.quit() .
Dropdowns, also known as select elements, are commonly used in forms to allow users to choose from
a list of predefined options.
Once again, as before, we use the click() method to begin interacting with the element. Once we've
clicked the button, we get a list of options. We use find_element() to find the selection we'd like to click,
and then we use click() to select the option.
After we opened the specified URL, we located the dropdown element with the ID "appointment-
time."
https://scrapeops.io/selenium-web-scraping-playbook/python-selenium-submit-form/ 6/20
3/31/24, 9:31 PM Python Selenium Guide - Submitting Forms with Selenium | ScrapeOps
The best choice usually depends on the page that you're working with. Depending on what you're doing
it may be more efficient click() the submit button, press the Enter key, use the submit() method,
or to use JavaScript Execution to submit the information.
Most importantly, you need to think, "What would a human do on this page?", the answer to that
question is most often the method that you should use. This is why we're using Selenium to begin with,
we want our program to behave like a human.
As you've done throughout this tutorial, to use the submit button, simply use find_element() to find the
button you're looking for and use click() to click on it.
Use this method when the form includes a clearly identifiable submit button, and interacting with
the button triggers the form submission.
Appropriate for standard HTML forms where the submit button is a standard input element with the
type "submit."
Suitable for cases where simulating user interactions is the primary objective.
https://scrapeops.io/selenium-web-scraping-playbook/python-selenium-submit-form/ 7/20
3/31/24, 9:31 PM Python Selenium Guide - Submitting Forms with Selenium | ScrapeOps
Selenium also allows you to call the submit() method on any form element, not just the submit button.
This method submits the form that contains the element.
Use this method when you have a direct reference to the form element, and calling the submit()
method on that element is a straightforward way to trigger submission.
Helpful in scenarios where submitting the form aligns with accessibility practices, as the submit()
method can trigger the default form submission behavior.
You can also submit information by sending the Enter key to the submit button. It works the same way
it would when using the Enter key in your browser.
Use this method when the form does not have a visible submit button, and pressing the Enter key
is a valid way to submit the form.
Appropriate for cases where the user typically submits the form by pressing Enter after typing in a
text input or textarea.
JavaScript Execution
Selenium also gives us the execute_script() method which allows us to insert JavaScript code directly
into our Python script.
https://scrapeops.io/selenium-web-scraping-playbook/python-selenium-submit-form/ 8/20
3/31/24, 9:31 PM Python Selenium Guide - Submitting Forms with Selenium | ScrapeOps
Use JavaScript execution when form submission involves complex logic, validations, or custom
event handling that may not be effectively triggered by standard Selenium methods.
Consider using JavaScript execution as a last resort when other methods fail or when dealing with
intricate web applications where standard methods may not suffice.
First, inside of the same folder as your scraper, create a new file, example.txt. and just add some
random text to it. Then save the file and add the following code to a Python script:
Start the browser and navigate to the site with webdriver.Chrome() and driver.get()
Find the upload element with driver.find_element(By.ID, "file-input-single")
Once we've found our element, we use
file_upload.send_keys(os.path.abspath("./example_file.txt"))
https://scrapeops.io/selenium-web-scraping-playbook/python-selenium-submit-form/ 9/20
3/31/24, 9:31 PM Python Selenium Guide - Submitting Forms with Selenium | ScrapeOps
The OS module gives us simple and universal way to find the absolute path to example.txt no matter
which operating system we''re using!
Next, we need to identify the sitekey of our captcha. You can do this with your developer console and
inspecting the page. You can view an example of this below.
Once you've found your sitekey, we'll make a Python script that uses both our API_KEY and site_key
as variables.
https://scrapeops.io/selenium-web-scraping-playbook/python-selenium-submit-form/ 10/20
3/31/24, 9:31 PM Python Selenium Guide - Submitting Forms with Selenium | ScrapeOps
https://scrapeops.io/selenium-web-scraping-playbook/python-selenium-submit-form/ 11/20
3/31/24, 9:31 PM Python Selenium Guide - Submitting Forms with Selenium | ScrapeOps
Troubleshooting
https://scrapeops.io/selenium-web-scraping-playbook/python-selenium-submit-form/ 12/20
3/31/24, 9:31 PM Python Selenium Guide - Submitting Forms with Selenium | ScrapeOps
When automating forms with Selenium, developers face a myriad of issues they run into that will cause
problems because at the end of the day, we're trying to simulate a human, but we're using a pre-
defined script that only does what we tell it to do.
If you pass an incorrect locator to find_element() or find_elements() , Selenium will look for the wrong
element, so it won't find the element that you're actually searching for.
To properly address this, we can need to ensure that our locators are correct and then use a proper
waiting strategy to find the element.
The code below uses an implicit wait while we interact with elements:
https://scrapeops.io/selenium-web-scraping-playbook/python-selenium-submit-form/ 13/20
3/31/24, 9:31 PM Python Selenium Guide - Submitting Forms with Selenium | ScrapeOps
Use driver.find_element() to find the search bar while implicitly waiting 10 seconds for it to appear
Enter "detroit" into the search bar with search_bar.send_keys("detroit")
Pause for 5 seconds using the sleep() function so we can view the screen
Sometimes, Selenium can find an element but we can't interact with it. To address this, you need to
know how your website behaves and what actions allow us to interact with the element.
For instance, if we are clicking a submit button, but the submit button isn't clickable until we've filled all
the required fields, we need to make sure that all the fields are filled and that the submit button is
clickable.
In the previous code example, if we try to click() any of our search results, we get an error because
the element is not interactable. In order to make it interactable, we need to hover our mouse over it. We
do this with ActionChains .
https://scrapeops.io/selenium-web-scraping-playbook/python-selenium-submit-form/ 14/20
3/31/24, 9:31 PM Python Selenium Guide - Submitting Forms with Selenium | ScrapeOps
action.move_to_element(target)\
.click()\
.perform()
#pause so we can view the screen
sleep(5)
driver.quit()
The example above is almost the same as the previous one, but with a few key differences:
After sending input to search_bar , we use find_elements() to find all search result elements
We iterate through the results with a for loop until we find out target result
Once we've found our target , we move to it and click on it with
action.move_to_element(target).click().perform()
When dealing with ActionChains , things can get very unreadable very quickly. We can use \ to
continue our code on a new line.
Occasionally, when we click() a button or send_keys() to an element, it simply won't work correctly!
When investigating issues like this, best practice is to use JavaScript execution in order to interact with
the element.
JavaScript has builtin DOM (Document Object Model) support for interacting with and manipulating
webpages. If JavaScript can't interact with your element, it is most likely a problem with the site and not
your scraper.
Let's pretend our previous method with ActionChains failed. We can use execute_script() to execute
native JavaScript and take advatage of JavaScript's first class DOM support.
https://scrapeops.io/selenium-web-scraping-playbook/python-selenium-submit-form/ 15/20
3/31/24, 9:31 PM Python Selenium Guide - Submitting Forms with Selenium | ScrapeOps
Sometimes we can fill a form out in its entirety, but it still won't submit. This actually happens quite often
when filling forms manually as well. In this case, first double check your scraper to make sure that your
fields are getting filled correctly.
In the event your fields are getting filled correctly, try different methods of submission that we went over
earlier in this article: Keys.ENTER , driver.execute_script() ...etc.
Let's use try and except to attempt different ways of clicking our result:
https://scrapeops.io/selenium-web-scraping-playbook/python-selenium-submit-form/ 16/20
3/31/24, 9:31 PM Python Selenium Guide - Submitting Forms with Selenium | ScrapeOps
Once again our code example is basically the same, here are the differences:
We see pop-ups all the time on the web... especially when submitting forms. Selenium has a builtin alert
methods such as dismiss() and accept() but these methods typically don't work.
More often than not, you will need to know the proper locators on the pop-ups or modals because they
are often custom made and the developer wanted people to have to manually click to dismiss them.
It is best practice to locate your accept, dismiss, or close button using proper waiting techniques and
then click() the proper button to handle the modal.
https://scrapeops.io/selenium-web-scraping-playbook/python-selenium-submit-form/ 17/20
3/31/24, 9:31 PM Python Selenium Guide - Submitting Forms with Selenium | ScrapeOps
As has been mentioned previously in this guide, quite often, we need to wait for items to properly
appear. Content that changes on the screen is called dynamic content. When dealing with dynamic
content we need to properly use either implicit or explicit waits for objects to appear.
When we use an implicit wait, we set a default timeout for any object to appear on the screen, as
long as the object appears within the waiting period, our code can interact with it
When using an explicit wait, we typically want to use it in combination with ExpectedConditions
so that webdriver can wait explicitly until the element appears on screen
The example below finds and closes the same pop-up we closed earlier, but this time we do it with an
explicit wait.
The differences you should notice from implicit wait code we've used throughout this article:
https://scrapeops.io/selenium-web-scraping-playbook/python-selenium-submit-form/ 18/20
3/31/24, 9:31 PM Python Selenium Guide - Submitting Forms with Selenium | ScrapeOps
While <iframe> elements aren't as commonly used as they once were do to security reasons,
sometimes they still get embedded into sites. To handle an iframe, we first use find_element() to find
our iframe element.
Once we've located it, we can use driver.switch_to.frame(name_of_iframe) to interact with that
specific element. To find elements from within the iframe, we can simply use find_element() or
find_elements() just like we would when interacting with the native webpage.
The code above executes just like any other Selenium script that we've used previously in this article but
with one main difference:
These issues are actually far more uncommon than they used to be. If you believe you might be having
compatibility issues, it is best to make sure that you have the latest browser driver installed. If
Chromedriver is up to date, and you're still running into issues with this, try Geckodriver and Firefox. If you
started out on Gecko with Firefox (and it is up to date) try with Chromedriver and Chrome.
https://scrapeops.io/selenium-web-scraping-playbook/python-selenium-submit-form/ 19/20
3/31/24, 9:31 PM Python Selenium Guide - Submitting Forms with Selenium | ScrapeOps
Conclusion
You now know how to properly submit form information with Selenium. Feel free to explore your new
superpower in the open web an see what you can do! You can automate actual form filling, or you
could even build your own chatbot that runs on the send_keys() and submit methods that we
discussed in this article!
If you'd like to learn more about Selenium, take a look at the Selenium Documentation, they small code
examples for Selenium in many different programming languages such as Python, Java, C#, Ruby,
JavaScript, and Kotlin.
https://scrapeops.io/selenium-web-scraping-playbook/python-selenium-submit-form/ 20/20