Identifying static dropdowns with Select class

Let us now discuss techniques to automate the static dropdowns in Selenium. If the options in a dropdown remain fixed they are considered as static. Next let us identify the dropdown by spying on it and then analyze its html code.

The dropdown Gender is identified and it has the select tagname. In Selenium webdriver, we have a Select class with which we interact with the static dropdown. To get access to the methods of the Select class to handle the options in the dropdown we have to create an object of the Select class. Also we have to import from selenium.webdriver.support.select import Select package to work with Select class in our code.

Selenium has to identify the specific dropdown and pass the locator of the dropdown as an argument to the Select class. Now to locate the dropdown we can use the id attribute which is having exampleFormControlSelect1 as value. Selenium shall identify that element with the help of its id attribute by the method find_element_by_id. The value of the id attribute shall be passed as an argument to the method.

Then we can use the method select_by_visible_text that selects the option as visible on the dropdown. The option visible is passed as an argument to the method. We can use the method select_by_index that selects the option based on the option index. The index starts from zero. The option index is passed as an argument to the method. We can also use the method select_by_value that selects the option based on the option value. The option value is passed as an argument to the method Please note, the method select_by_value can be used on if the value attribute is present for the option tag in the html code.

Code Implementation:

       from selenium import webdriver

       # setting the path of chromedriver.exe

       driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")

       # get method to hit URL on browser

       driver.get("https://rahulshettyacademy.com/angularpractice/")

       # identify element with css locator and then send input

       driver.find_element_by_css_selector("[name='name']").send_keys("Rahul")

       # identify element with name locator and then send input

       driver.find_element_by_name("email").send_keys("Shetty")

       # identify checkbox with id attribute and then select

       driver.find_element_by_id("exampleCheck1").click()

       #select class provide the methods to handle the options in dropdown

       d = Select(driver.find_element_by_id("exampleFormControlSelect1"))

       d.select_by_visible_text("Female")

       d.select_by_index(0)

      # identify element with xpath and then click

       driver.find_element_by_xpath("//input[@type='submit']").click()

      # identify element with class name and then get its text

       print(driver.find_element_by_class_name("alert-success").text)

Conclusion: 

Thus we have discussed how to handle static dropdowns using the Select class. For more details, you can refer to the link:

https://courses.rahulshettyacademy.com/courses/learn-selenium-automation-in-easy-python-language/lectures/13248286

In the next section we shall discuss how to perform validation assertions in Selenium with Python.

Introducing Validations Assertions

Now let us again work with the success message displayed on clicking the Submit button on the page https://rahulshettyacademy.com/angularpractice/.

We have successfully identified the message in the banner and fetched the text of the message with the method text and printed that in the console. To validate if the success message matches the expected value we shall use assertion. The assertion looks for the expected value and if it does not get it shall fail.

For example,

assert 2=2, will return true.

assert 2>3, will return false.

Now let us validate if the substring Success is present in the success message Success! The Form has been submitted successfully!. We will use assertion to conclude if the test case failed or passed.

 Code Implementation:

       from selenium import webdriver

       # setting the path of chromedriver.exe

       driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")

       # get method to hit URL on browser

       driver.get("https://rahulshettyacademy.com/angularpractice/")

       # identify element with css locator and then send input

       driver.find_element_by_css_selector("[name='name']").send_keys("Rahul")

       # identify element with name locator and then send input

       driver.find_element_by_name("email").send_keys("Shetty")

       # identify checkbox with id attribute and then select

        driver.find_element_by_id("exampleCheck1").click()

      # identify element with xpath and then click

       driver.find_element_by_xpath("//input[@type='submit']").click()

      # identify element with class name and then get its text

       msg = driver.find_element_by_class_name("alert-success").text

      # assertion to check if sub text gd is in Success message

       assert "gd" in msg

Output:

The output shows the AssertionError since the subtext gd is not present in the actual text Success! The Form has been submitted successfully!.

Let us now check if Success text is present in the actual success message.

 Code Implementation:

       from selenium import webdriver

       # setting the path of chromedriver.exe

       driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")

       # get method to hit URL on browser

       driver.get("https://rahulshettyacademy.com/angularpractice/")

       # identify element with css locator and then send input

       driver.find_element_by_css_selector("[name='name']").send_keys("Rahul")

       # identify element with name locator and then send input

       driver.find_element_by_name("email").send_keys("Shetty")

       # identify checkbox with id attribute and then select

        driver.find_element_by_id("exampleCheck1").click()

      # identify element with xpath and then click

       driver.find_element_by_xpath("//input[@type='submit']").click()

      # identify element with class name and then get its text

       msg = driver.find_element_by_class_name("alert-success").text

      # assertion to check if sub text Success is in message

       assert "Success" in msg

Output:

Conclusion:

 Thus we have discussed how to validate a text with assertion. For more details, you can refer to the link:

https://courses.rahulshettyacademy.com/courses/learn-selenium-automation-in-easy-python-language/lectures/13248290

In the next section we shall discuss how to handle auto suggestive dropdowns. Also, we shall see how to use the sleep method in a scenario.

Handling Auto Suggestive Dynamic dropdowns and Sleep method

In an auto suggestive dropdown the options are dynamic in nature and they are visible based on the search input by the user. To approach an auto suggestive dropdown we should first identify and store all the options with the find elements method in a list and then iterate over that list.

Now to locate the auto suggestive dropdown, we shall spy on it and analyze its html code. Please note that the element is having a tagname of input. Selenium shall identify that element with the help of its id attribute by the method find_element_by_id. The value of the id attribute shall be passed as an argument to the method. Finally to send some input, we shall be using the send_keys method and the text to be sent has to be passed as an argument to the method.

After a slight delay, the dropdown options will be populated and we shall add the method time.sleep to introduce the delay in the execution. The pause time in seconds is passed as an argument to the method and we have to import time which comes with the Python package. Next we shall create a generalized css expression li[class='ui-menu-item'] a which shall identify all the options that get displayed based on the search input.

Selenium shall identify the list of options by the method find_elements_by_css_selector. The css value for the list of options shall be passed as an argument to the method. We can validate the css for the list of options in the ChroPath, which shows 3 elements matching.

Finally we shall iterate over the list and once we encounter our desired option we shall select the option with the help of click method. Then we shall come out of the list iteration with the help of break statement.

The above example illustrates the find_element and find_elements in general terms where a single element is to be represented by find_element, a list of elements is to be represented by find_elements.

Code Implementation:

       from selenium import webdriver

       import time

       # setting the path of chromedriver.exe

       driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")

       # get method to hit URL on browser

       driver.get("https://www.rahulshettyacademy.com/dropdownsPractise/")

       # identify element with id locator and then send input

       driver.find_element_by_id("autosuggest").send_keys("ind")

       # to pause execution

       time.sleep(2)

       # identify list of options with a generalized css

       cntrs =driver.find_elements_by_css_selector("li[class='ui-menu-item'] a")

       # iterate over the list and scan the desired option

        for c in cntrs:

             if c.text =="India":

                   c.click()

                   break

Conclusion: 

Thus we have discussed how to handle auto suggestive dropdowns. For more details, you can refer to the link:

https://courses.rahulshettyacademy.com/courses/learn-selenium-automation-in-easy-python-language/lectures/13248291

In the next section we shall discuss how to use the len method to count a list of elements.

Get options count with Len method

Let us count the total number of options displayed after entering our search text for an auto suggestive dropdown. We have identified the list of options with the find_elements_by_css_selector method.

This method returns a list of matching elements and to get the count of the elements we shall take the help of len method. Then pass the name of the list as an argument to the method. Finally use the print statement to obtain the count of the options in the console log.

Code Implementation:

       from selenium import webdriver

       import time

       # setting the path of chromedriver.exe

       driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")

       # get method to hit URL on browser

       driver.get("https://www.rahulshettyacademy.com/dropdownsPractise/")

       # identify element with id locator and then send input

       driver.find_element_by_id("autosuggest").send_keys("ind")

       # to pause execution

       time.sleep(2)

       # identify list of options with a generalized css

       cntrs =driver.find_elements_by_css_selector("li[class='ui-menu-item'] a")

       # len method to get count list of options

         print (len(cntrs)) 

         # iterate over the list and scan the desired option

         for c in cntrs:

             if c.text =="India":

                   c.click()

                   break

Output: