Switching focus using Selenium WebDriver

Posted By :Rohitesh Rawat |29th June 2018

>>>Example showing how to switch focus on a window:

 

# Here, we are importing all the necessary modules
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

class SwitchToWindow():

    def test(self):
        baseUrl = "https://letskodeit.teachable.com/pages/practice"
        driver = webdriver.Firefox() # An instance of Firefox browser is created
        driver.maximize_window() #Window is maximized

        driver.get(baseUrl)

        # Find parent handle -> Main Window
        parentHandle = driver.current_window_handle
        print("Parent Handle: " + parentHandle)

        # Find open window button and click it
        driver.find_element(By.ID, "openwindow").click()
        time.sleep(2)

        # Find all handles, there should two handles after clicking open window button
        handles = driver.window_handles

        # Switch to window and search course
        for handle in handles:
            print("Handle: " + handle)
            if handle not in parentHandle:
                driver.switch_to.window(handle)
                print("Switched to window:: " + handle)
                searchBox = driver.find_element(By.ID, "search-courses")
                searchBox.send_keys("python")
                time.sleep(2)
                driver.close()
                break

        # Switch back to the parent handle
        driver.switch_to.window(parentHandle)
        driver.find_element(By.ID, "name").send_keys("Test Successful")






ff = SwitchToWindow()
ff.test()

 


>>>Example showing how to switch focus on a frame inside same page:

# Here, we are importing all the necessary modules
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

class SwitchToFrame():

    def test(self):
        baseUrl = "https://letskodeit.teachable.com/pages/practice"
        driver = webdriver.Firefox() # An instance of Firefox browser is created

        driver.maximize_window() #Window is maximized

        driver.get(baseUrl)
        driver.execute_script("window.scrollBy(0, 1000);")

        # Switch to frame using Id
        driver.switch_to.frame("courses-iframe")

        # Switch to frame using name
        # driver.switch_to.frame("iframe-name")

        # Switch to frame using numbers
        # driver.switch_to.frame(0)

        time.sleep(2)
        # Search course
        searchBox = driver.find_element(By.ID, "search-courses")
        searchBox.send_keys("python")
        time.sleep(2)

        # Switch back to the parent frame
        driver.switch_to.default_content()
        driver.execute_script("window.scrollBy(0, -1000);")
        time.sleep(2)
        driver.find_element(By.ID, "name").send_keys("Test Successful")




ff = SwitchToFrame()
ff.test()

 


>>>Example showing how to handle JavaScript Pop-up:

# Here, we are importing all the necessary modules
from selenium import webdriver
from selenium.webdriver.common.by import By
import time

class SwitchToFrame():

    def test1(self):
        baseUrl = "https://letskodeit.teachable.com/pages/practice"
        driver = webdriver.Firefox() # An instance of Firefox browser is created

        driver.maximize_window() #Window is maximized
        driver.get(baseUrl)


        driver.find_element(By.ID, "name").send_keys("Anil")
        driver.find_element(By.ID, "alertbtn").click()
        time.sleep(2)
        alert1 = driver.switch_to.alert
        alert1.accept()
        time.sleep(2)
        driver.find_element(By.ID, "name").send_keys("Anil")
        driver.find_element(By.ID, "confirmbtn").click()
        time.sleep(2)
        alert2 = driver.switch_to.alert
        alert2.dismiss()



ff = SwitchToFrame()
ff.test1()

 


About Author

Rohitesh Rawat

Rohitesh is an expert in Agile methodologies, specializing in Scrum. He possesses a wide range of skills, including proficiency in Jira, MongoDB, planning, scoping, process creation and management, and QA. Over the years, he has led the successful delivery of several offshore projects, including Konfer, Virgin Media, HP1T, and Transleqo. Rohitesh holds certifications as a Certified Scrum Master (CSM) and Project Management Professional (PMP) and has a comprehensive understanding of the entire Project Life Cycle (PLC).

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us