Open In App

How to access popup login window in selenium using Python

Last Updated : 22 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Many websites use sign-in using social media to make the login process easy for users. In most cases, if the button is clicked then a new popup window is opened where the user has to enter their user credentials. Manually one can switch windows in a browser and enter the required credentials to log in. But in case of unattended web access using a web driver, the driver can not just switch the windows automatically. We need to change the window handle in the driver to enter the login credentials in the popup window. Selenium has the function to switch the window to access multiple windows using the same driver.
First, we have to get the current window handle from a web driver which can be done by: 

driver.current_window_handle

We need to save it to get to the current window handle. After the popup window appears we have to get the list of all the window handles available right now. 

driver.window_handles

Then we can get the window handle of the login page from this list and then switch the control. To switch the window handle, use: 

driver.switch_to.window(login_page)

After successful login we can use the same switch_to method to change control to the previous page. 

Note: To run this code selenium library and geckodriver for firefox is required. The installation of selenium can be done using Python third-party library installer pip. To install selenium run this command 

pip install selenium

For geckodriver, download the file and add it’s path to the OS PATH variable, so that it can be activated from anywhere in file directory.
Let’s see the code for login on zomato.com using Facebook.
 

Python3
# import the libs
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By

# create the initial window
driver = webdriver.Firefox()

# go to the home page
driver.get('https://www.zomato.com')

# storing the current window handle to get back to dashboard
main_page = driver.current_window_handle

# wait for page to load completely
sleep(5)

# click on the sign in tab
driver.find_element(By.XPATH, '//*[@id ="signin-link"]').click()

sleep(5)

# click to log in using facebook
driver.find_element(By.XPATH, '//*[@id ="facebook-login-global"]/span').click()

# changing the handles to access login page
for handle in driver.window_handles:
    if handle != main_page:
        login_page = handle
        
# change the control to signin page        
driver.switch_to.window(login_page)

# user input for email and password
print('Enter email id : ', end ='')
email = input().strip()
print('Enter password : ', end ='')
password = input().strip()

# enter the email
driver.find_element(By.XPATH, '//*[@id ="email"]').send_keys(email)

# enter the password
driver.find_element(By.XPATH, '//*[@id ="pass"]').send_keys(password)

# click the login button
driver.find_elemenT(By.XPATH, '//*[@id ="u_0_0"]').click()



# change control to main page
driver.switch_to.window(main_page)

sleep(10)
# print user name 
name = driver.find_elemenT(By.XPATH, '/html/body/div[4]/div/div[1]/header/div[2]/div/div/div/div/span').text
print('Your user name is : {}'.format(name))

# closing the window
driver.quit()

Output: 
 


 


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads