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:

The output shows 3 meaning the count of the number of options is three.

Conclusion: Thus we have discussed how to use the len method to count a list of elements. For more details, you can refer to the link:

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

In the next section we shall discuss how to handle checkboxes dynamically.

Handling Checkbox dynamically

Let us now see how to handle checkboxes on a page and navigate to the link: https://rahulshettyacademy.com/AutomationPractice/. We shall spy on the checkbox and analyze its HTML code.

Now if we observe the properties for all the checkboxes, we shall find that except the type = checkbox all the other properties have a different value. We can create a generalized xpath expression //input[@type='checkbox'] that shall identify all the checkboxes. Selenium shall identify the list of checkboxes by the method find_elements_by_xpath. The xpath value representing all the checkboxes shall be passed as an argument to the method. We can validate the xpath expression in the ChroPath, which shows 3 elements matching.

Finally we shall iterate over the list of checkboxes and we shall select them one by one with the help of click method. We can validate if the checkbox is selected with the is_selected method. This method returns a true value for a selected checkbox and returns false value for an unselected checkbox. 

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/AutomationPractice/")

       # identify generalized xpath for checkboxes

       chkbxs  = driver.find_elements_by_xpath("//input[@type='checkbox']")

       # iterate over the list and select them

       for c in chkbxs:

            c.click()

           # validate if checkbox is selected

            assert c.is_selected()

Please note, if we have used the find_element_by_xpath method with a generalized xpath that represents all the checkboxes on the page, then only the first checkbox gets selected.

Conclusion:

 Thus we have discussed how to handle checkboxes dynamically. For more details, you can refer to the link:

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

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

In the next section we shall discuss how to handle radio buttons.

Handling Radio button

Let us now discuss how to handle radio buttons on a page and navigate to the link: https://rahulshettyacademy.com/AutomationPractice/. We shall spy on the radio button and analyze its HTML code.

Now if we observe the properties for all the radio buttons, we shall find that except the type = radio and name = radioButton all the other properties have a different value. By nature, all the radio buttons on the page cannot be clicked at the same time. Selenium shall identify the radio buttons with the help of its name attribute by the method find_elements_by_name. The value of the name attribute shall be passed as an argument to the method.

Finally we can access the list of radio buttons with the help of index. We can select one of the radio buttons with the help of click method. We can validate if the radio button is selected with the is_selected method. This method returns a true value for a selected radio button and returns false value for an unselected radio button.

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/AutomationPractice/")

       # identify radio buttons

       radiobuttons  = driver.find_elements_by_name("radioButton")

       # select third radio button with list index of 2

       radiobuttons[2].click()

       # validate if radio button is selected

       assert radiobuttons[2].is_selected()

Conclusion: 

Thus we have discussed how to handle radio buttons. For more details, you can refer to the link:

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

In the next section we shall discuss how to handle Java/Javascript alert.

Handling Java/Javascript alerts pop up

Let us now discuss how to Java/Javascript alerts in a page and navigate to the link: https://rahulshettyacademy.com/AutomationPractice/. We cannot spy on the alert pop as it is not part of HTML code and implemented with Java/Javascript.

We shall input some text in the edit box above the Alert button and on clicking the button, we shall get the alert pop. We shall then verify the alert pop text.

In Selenium, the driver object cannot directly act on the alert. We have to switch from the driver to the alert mode with the help of switch_to.alert method and store it in an alert object. We shall be able to access all the alert methods with the alert object.

To get the alert text we shall use the text method. Next to accept that alert we shall use the accept method. Finally we can validate the alert text with the help of assertion.

Code Implementation:

       from selenium import webdriver

       validateText = "Option3"

       # 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/AutomationPractice/")

       # identify edit box and send input

       driver.find_element_by_css_selector("#name").send_keys(validateText)

       # identify Alert button and then click

       driver.find_element_by_id("alertbtn").click()

       # create alert object and switch to it

       alert = driver.switch_to.alert()

       # get the alert text and validate with assertion

       alertText = alert.text      

       assert validateText in alertText

       #to accept alert    

       alert.accept()

Let us explore alert pop ups with both OK and Cancel buttons. To dismiss the alert we shall need to click the Cancel button. The driver object cannot directly act on the alert. We have to switch from the driver to the alert mode with the help of switch_to.alert method and store it in the alert object. We shall then be able to access all the alert methods with that object.

Next to dismiss that alert we shall use the dismiss method.

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/AutomationPractice/")

       # identify Confirm button and then click

       driver.find_element_by_id("confirmbtn").click()

       # create alert object and switch to it

       alert = driver.switch_to.alert()

       # get the alert text

       alertText = alert.text      

       #to dismiss alert    

       alert.dismiss()

Conclusion: 

Thus we have discussed how to handle Java / Javascript alert. For more details, you can refer to the link:

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

In the next post, we shall discuss synchronization.


Tags


You may also like

Groups attribute in TestNG 

Groups attribute in TestNG 
Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}