While Testing web application, We generally encounter Alert box, Select drop downs. Let’s see How we can handle them with selenium web driver in python.
Generally, JavaScript popups are generated by the web application and hence they can be easily controlled by the browser.
Case1: Handling the Alert/Pops:
Create a Selenium Project and in the test Method write the below code to handle the Alert box/Pop. Just get the element using the
”find element by xpath method” and use
1 2 3 4 | alertObj = driver.switch_to.alert alertObj.accept() # To dismiss alertObj.dismiss() |
Case2: Handling the (Select)DropDown:
Create a Selenium Project and in the test Method write the below code to handle the select dropdown. Just get the element using the
”find element by xpath or id method” and use
1 2 3 4 5 | def test_waitForCheckOutPhotosButton(self): dropDownId= "wsite-com-product-option-Quantity" dropDownElement = WebDriverWait(driver, 10). until(lambda driver : driver.find_element_by_id(dropDownId)) Select(dropDownElement).select_by_value("2")'' |
Case3: Handling the Window Frame:
Lets assume that, When you click on button1(id=’button1’) on the main window a new window pop opens and you have switch to that window and click the button(id=’window1_button2’) on it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | def test_waitForCheckOutPhotosButton(self): button1 = WebDriverWait(driver,10) .until(lambda driver: driver.find_element_by_id("button1")) #Getting the Main Window Handle mainHanlde = driver.window_handles button1.click() #Getting all windows Main and Window 1 handle in All Handle allHandle = driver.window_handles for c in allHandle: if c !=mainHanlde[0]: driver.switch_to_window(c) break button2= button1 = WebDriverWait(driver,10) .until(lambda driver: driver.find_element_by_id("window1_button2")) |
We will learn about how to use Action chain with Selenium in Python in next tutorial.
Check out the Ultimate guide to Cross Browser Testing.
which IDE is better to work with python either eclipse or pychram
How to add the values in popup window using Python(3.6) selenium
Same as given in the post
Thanks for sharing all the information with us all.