Selenium Webdriver - Forms



Selenium webdriver can be used to submit a form. A form in a page is represented by the <form> tag. It contains sub-elements like the edit box, dropdown, link, and so on. Also, the form can be submitted with the help of the submit method.

The syntax for forms is as follows −

src = driver.find_element_by_css_selector("#draggable")
src.submit()

Let us see the html code of elements within the form tag.

HTML Code of Elements

On submitting a form with the above html code, the below alert message is displayed.

HTML Code

Code Implementation

The code implementation for submitting a form is as follows −

from selenium import webdriver
from selenium.webdriver.common.alert import Alert
driver = webdriver.Chrome(executable_path='../drivers/chromedriver')
#implicit wait time
driver.implicitly_wait(5)
#url launch
driver.get("https://www.tutorialspoint.com/selenium/selenium_automation_practice.htm")
#identify element within form
b = driver.find_element_by_name("firstname")
b.send_keys('Tutorialspoint')
e = driver.find_element_by_name("lastname")
e.send_keys('Online Studies')
#submit form
e.submit()
# instance of Alert class
a = Alert(driver)
# get alert text
print(a.text)
#accept alert
a.accept()
#driver quit
driver.quit()

Output

External Pages

The output shows the message - Process with exit code 0 meaning that the above Python code executed successfully. Also, the Alert text - You are submitting information to an external page.

Are you sure?

The above message gets printed in the console.

Advertisements