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 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 webdriver, 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 have 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 webdriver which can be done by:
We need to save it in order to get the to the current window handle. After the popup window appears we have to get the list of all the window handles available right now.
Then we can get the window handle of the login page from this list and then switch the control. To switch window handle, use:
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.
- Gmail Login using Python Selenium
- Facebook Login using Python
- Python - popup menu in wxPython
- Python | Popup widget in Kivy
- Python | Popup widget in Kivy using .kv file
- Django Sign Up and login with confirmation Email | Python
- Create MySQL Database Login Page in Python using Tkinter
- How to get current_url using Selenium in Python?
- How to take screenshot using Selenium in Python ?
- Selenium Python Tricks
- Exceptions - Selenium Python
- Python | SMS Bomber using Selenium
- How to install Selenium in Python
- Selenium Python Tutorial
- Waits in Selenium Python
- Selenium Python Basics
- Web Driver Methods in Selenium Python
- Implicit Waits in Selenium Python
- Action Chains in Selenium Python
- Search Google Using Python Selenium
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.