- Selenium Webdriver Tutorial
- Home
- Introduction
- Installation
- Browser Navigation
- Identify Single Element
- Identify Multiple Elements
- Explicit and Implicit Wait
- Pop-ups
- Backward and Forward Navigation
- Cookies
- Exceptions
- Action Class
- Create a Basic Test
- Forms
- Drag and Drop
- Windows
- Alerts
- Handling Links
- Handling Edit Boxes
- Color Support
- Generating HTML Test Reports in Python
- Read/Write data from Excel
- Handling Checkboxes
- Executing Tests in Multiple Browsers
- Headless Execution
- Wait Support
- Select Support
- JavaScript Executor
- Chrome WebDriver Options
- Scroll Operations
- Capture Screenshots
- Right Click
- Double Click
- Selenium Webdriver Useful Resources
- Selenium WebDriver - Quick Guide
- Selenium WebDriver - Useful Resources
- Selenium WebDriver - Discussion
Selenium Webdriver - Create a Basic Test
To create a basic test in Selenium with Python, the below steps need to be executed −
Step 1 − Identify the browser in which the test has to be executed. As we type webdriver in the editor, all the available browsers like Chrome, Firefox get displayed. Also, we have to pass the path of the chromedriver executable file path.
The syntax to identify the browser is as follows −
driver = webdriver.Chrome(executable_path='<path of chromedriver>')
Step 2 − Launch the application URL with the get method.
The syntax for launching the application URL is as follows −
driver.get("https://www.tutorialspoint.com/about/about_careers.htm")
Step 3 − Identify webelement with the help of any of the locators like id, class, name, tagname, link text, partial link text, css or xpath on the page.
The syntax to identify the webelement is as follows:
l = driver.find_element_by_partial_link_text('Refund')
Step 4 − After the element has been located, perform an action on it like inputting a text, clicking, and so on.
The syntax for performing an action is as follows:
driver.find_element_by_partial_link_text('Refund').click()
Step 5 − Finish the test by quitting the webdriver session. For example,
driver.quit();
Let us see the html code of a webelement −
The link highlighted in the above image has a tagname - a and the partial link text - Refund. Let us try to click on this link after identifying it.
Code Implementation
The code implementation to create a basic test is as follows &minus'
from selenium import webdriver driver = webdriver.Chrome(executable_path='../drivers/chromedriver') #url launch driver.get("https://www.tutorialspoint.com/about/about_careers.htm") #identify link with partial link text l = driver.find_element_by_partial_link_text('Refund') #perform click l.click() print('Page navigated after click: ' + driver.title) #driver quit driver.quit()
Output
The output shows the message - Process with exit code 0 meaning that the above Python code executed successfully. Also, the page title of the application (obtained from the driver.title method) - Return, Refund & Cancellation Policy - Tutorialspoint gets printed in the cons
To Continue Learning Please Login
Login with Google