Handle Dropdown and Alerts Using Selenium Python

Introduction

In this article, we will understand how to handle dropdown and alerts using selenium with python.

Prerequisite

Need to set up selenium with python project.

In Html,the select tag is used to create the dropdown

Select tags can contain one or more <option> tags as its children, each representing an item in the dropdown. The <option> tags define the individual options that users can choose from in the dropdown.

Inorder to handle dropdowns we need to make use of select class in selenium.

Select class from the webdriver.support.ui.selects module.

create a select class object.

Pass the reference of the webelement to the select class.

Now use the select class object and call the below functions.

  1. select_by_visible_text
  2. select_by_value
  3. select_by_index

select_by_visible_text

This method will match with the visible text attribute so that you can click the dropdown value for which it becomes matched.

select_by_value

we need to pass a text as a parameter to select_by_value. Then it will match with the value attribute text so that the user can click the dropdown value for which it becomes matched.

select_by_index

Index values are started from 0 and continue with an increment of +1. It means if there are five dropdown values then they will be 0,1,2,3, and 4. Suppose, you want to select the 3rd value from the dropdown then the index will be 2.

Code with explanation comments.

# importing the modules
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.common.alert import Alert
import time

# create chrome driver instance
driver=webdriver.Chrome()

# opening the webpage
driver.get("https://demo.automationtesting.in/Register.html")

# using findelement to locate the element in the webpage and storing the reference in a variable 
dropdownelement= driver.find_element(By.ID,'Skills')

#passing the webelement to the select class
drop=Select(dropdownelement)

# use the select class object and choose select by visible text method
drop.select_by_visible_text("Android")

# time is a package in python which is used to put hard waits in the scripts. it will wait for 4 seconds before the next line is going to be executed
time.sleep(4)


# select by index
drop.select_by_index(6)
time.sleep(4)

# select by value
drop.select_by_value('Certifications')
time.sleep(4)

# close the browser session
driver.close()

We can handle alerts in python using the below functions.

  • accept(): Accepts the alert available.
  • dismiss(): Dismisses the alert available.
  • send_keys(keysToSend): Send Keys to the Alert. This method will type the value in the alert which has a prompt text box in it.
  • text: Gets the text of the Alert.

We need to create alert object.

  • For using alert we need to import the package
  • from selenium.webdriver.common.alert import Alert

al=Alert(driver)

Alert(driver) code is used to switch the WebDriver's focus to an alert dialog that might be present on the web page. It creates an Alert object that allows you to interact with the JavaScript alert, prompt, or confirmation dialogs.

Code with explanation comments.

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.common.alert import Alert
import time

driver=webdriver.Chrome();

driver.get("https://demo.automationtesting.in/Alerts.html")

element=driver.find_element(By.ID,"OKTab")

element.click()

# creating the alert object
al=Alert(driver)

# print the alert text
print(al.text)

# accept the alert
al.accept()

secondele=driver.find_element(By.CSS_SELECTOR,"li a[href='#CancelTab']")
secondele.click()

alertbutton=driver.find_element(By.ID,"CancelTab")
alertbutton.click()

#print the text in the alert
print(al.text)

# dismiss the alert
al.dismiss()

Thanks

Happy learning....


Similar Articles