Front-end Testing in Python: A Detailed Guide
Fill and Submit with Forms
Once you’ve been able to select a form or an input element, you can now fill and submit it. To fill a form through Selenium, you need to emulate keystrokes on your keyboard. You need to make an additional import as shown below to perform this operation:
from selenium.webdriver.common.keys import Keys
Let’s now fill the search form and submit it:
search_bar = driver.find_element_by_id("id-search-field")search_bar.clear()search_bar.send_keys("django")search_bar.send_keys(Keys.RETURN)
If you’re performing this test on Chrome, you’ll notice the form being filled in through Selenium, and the form being submitted. Once the search results are displayed, you can verify if the URL of the current page has changed with the following:
print(driver.current_url)
The output should reflect the same URL that you can see in the open browser window:
https://www.python.org/search/?q=django&submit=