Selenium Navigation - Form Submission
Navigations in selenium can be done in different ways. In this tutorial, we will use the form submission functionality.
But before that, please make sure you have read the
first blog on this
series to do the prerequisites.
Selenium Navigation Using Form Element Submit
- Create a file seleniumnavform.py and paste the following codes
from selenium import webdriver import time
The codes above imports the required library that we will use. - Add this line
driver = webdriver.Firefox(executable_path="geckodriver.exe")
The code above will create a webdriver instance for Firefox. - Add this line
driver.get("https://slackingslacker.github.io/seleniumindex#/forms")
The line will go to the website (https://slackingslacker.github.io/seleniumindex#/forms). - Add this line
time.sleep(5)
We will pause the program for 5 seconds. - Add this line
el = driver.find_element_by_css_selector("div > form")
This code will find an element using a CSS selector. The code will find a form element that is a direct child of a div. - Add this line
el.submit()
This will submit form. - Add this line
time.sleep(10)
We will pause the program for 10 seconds to see that it submitted the form. - Add this line
driver.close()
The line will close the webdriver as well as the browser. - Run the seleniumnavform.py. It should do the following:
- Open the firefox browser
- Browser goes to https://slackingslacker.github.io/seleniumindex#/forms
- Halts for 5 seconds
- Finds the form tag
- Submits the form without click a button
- Halts for 10 seconds
- Closes the browser
Final Selenium Code
from selenium import webdriver import time driver = webdriver.Firefox(executable_path="geckodriver.exe") driver.get("https://slackingslacker.github.io/seleniumindex#/forms") time.sleep(5) el = driver.find_element_by_css_selector("div > form") el.submit() time.sleep(10) driver.close()
No comments:
Post a Comment