How to handle "Plugin blocked" pop up using Selenium Python?

SeleniumAutomation TestingTesting Tools

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()

Output

raja
Published on 08-Apr-2021 07:26:15
Advertisements