Closing a generic pop up in Selenium
Asked Answered
I

5

5

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?

Iveyivie answered 3/12, 2014 at 11:16 Comment(1)
Louis is absolutely correct, its not an alert window that can be handled using switch to alert. So you need to close the pop-up div using close button.Anywheres
F
7

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()
Feliciafeliciano answered 3/12, 2014 at 11:47 Comment(2)
Can you also describe how you built that specific xpath ? In usually use inspect element & then copy xpath- but I got a different xpath. Also, I have used xpath checker in past, but it doesnt work in all cases.Iveyivie
Above is a relative xpath unlike the absolute xpath that we (sometimes) get using the "Firepath" in Firebug. It is used for creating a consistent xpath and is most likely to be effective in the long run. You can see this link, it will help you out in making your own relative xpath(s): https://www.simple-talk.com/dotnet/.net-framework/xpath,-css,-dom-and-selenium-the-rosetta-stone/Feliciafeliciano
M
6

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)
Mahoney answered 22/11, 2017 at 14:11 Comment(0)
D
2

The popup you are trying to close is not a browser alert but a DOM popup. A browser alert is a dialog box that the browser creates at the OS level. (It is appears as an additional window on your desktop.) You can use .switch_to_alert() to close these alerts. A DOM popup is just an HTML element that happens to be used the the page as if it were a dialog box. This popup has existence only in your browser. (The OS does not know about it.) You cannot use .switch_to_alert() to close DOM popups.

To get this popup and close it, you have to inspect the DOM (like you've started doing), find the DOM element that represents the close button and have Selenium click it.

Defeatist answered 3/12, 2014 at 11:21 Comment(0)
E
0

I ran into a similar problem, except that our pop-up's closebox webelement had a dynamic id tag, making it difficult to latch onto. We ended up solving it by using By.className to find it. In the code below, I use a List because I was also looking to see how many elements I was dealing with, but that's not required. Also, the production code has additional handling in case window closebox elements were found.

List<WebElement> closeboxes = driver.findElements(By.className("v-window-closebox"));
for (WebElement we : closeboxes) {
  we.click();
}
Era answered 14/5, 2015 at 19:41 Comment(0)
I
0

You can try the following:

from selenium.webdriver import ActionChains, Chrome


driver = Chrome('/usr/local/CHROMEDRIVER')
ActionChains(driver).move_to_element_with_offset(
    driver.find_element_by_xpath('//html'), 0, 2338
    ).click().perform()

This will click an area outside of the popup thus closing it. 🤠

Inquisition answered 1/1, 2022 at 23:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.