Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing a generic pop up in Selenium

While scraping a page using selenium webdriver, there is a "pop up" that appears .

On Opening the page, http://www.fanatics.com/search/red%20shoes - I see a popup window with xpath '//*[@id="mm_DesktopInterstitialImage"]' - but I don't want to be using the xpath to close this alert, and have something genric that can dismiss/close the alert. Here's what I tried so far -:

from selenium import webdriver
import os
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

chromedriver = "/usr/local/CHROMEDRIVER"
desired_capabilities=DesiredCapabilities.CHROME
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver,desired_capabilities=desired_capabilities)
url='http://www.fanatics.com/search/red%20shoes'
driver.get(url)
#driver.set_page_load_timeout(300)
driver.implicitly_wait(60)
alert = driver.switch_to_alert()
alert.dismiss
handle=driver.window_handles
print handle
#[u'CDwindow-B1E9C127-719D-ACAA-19DE-1A6FA265A4FF']

From what I understand from related examples, folks usually switch window handles, but my handle has a single element.i.e, driver.switch_to_window(driver.window_handles[1]) then driver.close() and finally shift again, using driver.switch_to_window(driver.window_handles[1]) I also used implicit wait, since I was not sure, if the alert window was being read at-all - but that did not work either. I do not wnat to hard-code for the xpath,if that is possible.

What am I doing wrong ?

Related, but does't work for me : Selenium python how to close a pop up window?

like image 622
ekta Avatar asked Dec 14 '22 18:12

ekta


2 Answers

As I can see, it's not an alert box!!. It is just a Simple Pop-up that appears when you are entering the page and it is present in main window itself(no need of switching and closing it too). Use the below code to close it.

driver.find_element_by_xpath('//div[contains(@class,"ui-dialog") and @aria-describedby="dialogContent2"]//button[@title="Close"]').click()
like image 90
Subh Avatar answered Dec 18 '22 11:12

Subh


It works perfectly, give it a try ;)

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("--disable-notifications")
browser = webdriver.Chrome('C:\Python34\Lib\site-packages\selenium\webdriver\chromedriver.exe', chrome_options=options)
like image 35
Vedanta6 Avatar answered Dec 18 '22 10:12

Vedanta6