
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Handle Plugin Blocked Pop-Up Using Selenium with Python
We can handle plugin popup using Selenium webdriver in Python. Whenever a popup comes on page, we cannot inspect elements within the popup and identify them.
Also, in order to access other elements on the page, we have to first either accept default has access to the main page. To interact with the popup, we have to explicitly shift the driver focus with the help of the switch_to.alert() method.
The popup mainly consists of a message along with Ok and Cancel buttonsto accept and dismiss a popup respectively. To accept a popup, the method switch_to.alert().accept() is used.
To dismiss a popup, the method switch_to.alert().dismiss() is used. To obtain the text on a popup, we have to use the switch_to.alert().text method.
Syntax
driver.switch_to.alert.text driver.switch_to.alert.accept() driver.switch_to.alert.dismiss()
Let us make an attempt to obtain the text on the popup.
Example
from selenium import webdriver from selenium.webdriver.common.alert import Alert #set chromodriver.exe path driver = webdriver.Chrome(executable_path="C:\chromedriver.exe") driver.implicitly_wait(0.5) #launch URL driver.get("http://www.uitestpractice.com/Students/Switchto") #identify element m = driver.find_element_by_id("confirm") m.click() #switch to popup driver.switch_to.alert #obtain text p = driver.switch_to.alert.text print("Text is: ") print(p) #accept popup driver.switch_to.alert.accept() m.click() #dismiss popup driver.switch_to.alert.dismiss() #close browser driver.close()