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 −

Refund Policy

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

Return Refund and Cancellation Policy

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

Advertisements