How To Handle Child Window, Frames, Alerts in Selenium Python
In the previous articles on Selenium Python Tutorial, we have covered “Webelement commands in Selenium Python“. In this tutorial, we will learn How To Handle Child Window, Frames, Alerts in Selenium Python.Â
Selenium has a unique way of handling Javascript alerts. The alerts are pop-ups that appear on clicking a link or a button. Immediately after it appears on the page, the focus is shifted from the current page to the alert.
Selenium WebDriver has multiple APIs to work with the alerts and to perform actions like accepting, dismissing, getting the alert text, and so on. We need to either accept or dismiss the alerts to continue our task on the main page.
An alert cannot be identified by inspecting them. Also, we cannot create customized XPath for alerts. While the alert is present on a page and if we try to access any element on the main page, AlertPresentException is thrown. Once an alert pop up appears on a page, the driver focus still remains on the main webpage.
To work with an alert pop up we need to switch the driver focus from the parent page to the pop up generated. This is achieved with the help of the switch_to.alert () method.
Syntax:
a = driver.switch_to.alert ();
After switching the focus on the alert, we can accept, dismiss, or get the text displayed in the alert.
For accepting the alert or clicking on the OK button, we have to use the method as a.accept (). Next to dismiss or to click on the X symbol, the alert method to be used is a.dismiss (). Sometimes if we want to extract text on the pop-up, the method to be used is a.text. Also, we can input text on the alert with the a.send_keys () method. The text to be entered is passed as a parameter to the method.
Code Implementation with Alert.
# import the webdriver from selenium import webdriver # import the Keys class from selenium.webdriver.common import keys driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method to launch the URL driver.get("http://softwaretestingplace.blogspot.com/2017/03/javascript-alert-test-page.html") # to identify the element I = driver.find_element_by_xpath ("//*[@id = 'content']/button") # to click the button l.click() # to switch to the alert a = driver.switch_to.alert (); # to get the alert text print ( a.text) # to accept the alert a.accept () # again producing the alert I = driver.find_element_by_xpath ("//*[@id = 'content']/button") # to click the button l.click () # to dismiss the alert a.dismiss () # to close the browser driver.close ()
Frames
We can handle frames with Selenium WebDriver. In an HTML document, we can have web elements that are a part of another webpage. The frame in an HTML document is described inside a <frame> or <iframe> tag. If there are multiple frames, they are part of the <frameset> tag.
A frame can be identified in a web page with the help of its name, id, web element, or the index. To access an element inside a frame, we have to switch the focus from the main page content to the specific frame where the element is present.
Let us consider a sample HTML code with frame/ iframe tag.
< frame id="fid" name="fname" class='fclass' > </frame> < frame id="fid1" name="fname2" class='fclass2' ></frame>
To switch to a frame/ iframe with the value of the name attribute in the HTML, the syntax is:
driver.switch_to.frame ("fname")
To switch to a frame/ iframe with the value of the id attribute in the HTML, the syntax is:
driver.switch_to.frame ("fid")
We can switch to a frame/ iframe with the help of the locators like CSS or XPath. The locators like link text and partial link text do not work with the frames. Also, if the page contains multiple frames, then the tagname locator will not be able to identify a particular frame/ iframe we are looking for.
To switch to a frame/ iframe using css, the syntax is driver.switch_to.frame (“frame[class = ‘fclass’] “). The syntax with xpath is driver.switch_to.frame (“frame [@class = ‘fclass’] “).
To switch to a frame/ iframe with index, the syntax is:
driver.switch_to.frame (0)
The index of a frame/ iframe starts from 0. All the frames/ iframes on a web page are assigned with an index.
To switch the focus from all the frames/ iframes and move to the main web page, the method to be used is switch_to.default_content (). Once we shift out of the frame we can no longer access the elements inside the frame.
Code Implementation with frame.
# import the webdriver from selenium import webdriver # import the Keys class from selenium.webdriver.common import keys driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method to launch the URL driver.get ("https://the-internet.herokuapp.com/nested_frames ") # to switch to a frame with name attribute driver.switch_to.frame ("frame-bottom") # to identify an element inside that frame and get text print ( driver.find_element_by_css_selector ("body").text) # to switch to the main page content driver.switch_to.default_content () # to close the browser driver.close ()
Child Windows
We can handle child windows with the help of Selenium web drivers. Often on clicking a link or a button, a new tab opens and we need to continue our testing on the other browser that has opened.
Once a new pop up window gets opened, we have to switch from one pop up window which is in focus to the other pop up window. Each of these windows has a unique id.
Let us discuss some of the methods to work with multiple windows:
current_window_handle – This method is used to fetch the window handle id of the window in focus in the form of a string value.
window_handles – This method is used to fetch all the handle ids of the windows currently opened in the form of a set data structure.
switch_to.window (args) – This method is used to shift the focus from one pop up window to another. The window to be switched to is passed as a parameter to that method.
Code Implementation with multiple windows.
from selenium import webdriver # setting the property for chromedriver.exe driver = webdriver.Chrome (executable_path="C:\\chromedriver.exe") # get method for launching the application driver.get ("http://www.naukri.com/") # to maximize the window of the browser driver.maximize_window () # to get the parent window handle c = driver.current_window_handle # to get all opened windows wnds = driver.window_handles # to iterate through the window handles for i in wnds: #switch focus to child window if(i!=c): driver.switch_to.window (i) break time.sleep (0.5) print ("The Current window title: " + driver.title) #switch focus to main parent window driver.switch_to.window(c) # to close the browser driver.quit ()
In the next article, we will learn How To Handle Mouse and Keyboard Interactions in Selenium Python
Related posts:
- Selenium Python Tutorial
- Selenium Java Tutorial
- Selenium Interview Questions
- API Testing Tutorial
- Postman Tutorial