Nuffsaid
Nuffsaid

Reputation: 41

Using Selenium to close a browser pop-up

I'm scraping some data from a website (Investing.com) and occasionally get a pop-up. I've looked for a clickable button in the elements but can't find anything.

Here is the segment from the element page, where the <"i"> element is all I can find relating to the 'X' to close.

...
<div class="right">
    <i class="popupCloseIcon largeBannerCloser">
        ::after
    </i>
...

Code I've tried is below:

from selenium.webdriver import ChromeOptions as Options
import selenium.webdriver as webdriver

chrome_options = Options()
#chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')

d = webdriver.Chrome('chromedriver', options=chrome_options)
d.get("https://www.investing.com/currencies/eur-usd-technical")

iElements = d.find_elements_by_tag_name("i")
for name in iElements:
    print(name.get_attribute("class"))
    if name.get_attribute("class") == "popupCloseIcon largeBannerCloser":
        print("im here")
        name.click()

I print my text and get there, so know I've found my element, but I end up with the following error:

selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

Can't find anything else to interact with though?

Upvotes: 0

Views: 1825

Answers (2)

Nuffsaid
Nuffsaid

Reputation: 41

Found that I could bypass this by deleting the div element that contains all of the banner. Managed to located this by it's CSS selector and then delete it using some Javascript (copied/pasted).

This then allows my clicks to register so that I can get my elements as required.

Code below tries waiting for the pop-up for 5 seconds, in which case it deletes the element, otherwise it continues trying to click.

delay = 5
try:
    myElem = WebDriverWait(d, delay).until(EC.presence_of_element_located(
        (By.CSS_SELECTOR, 'body > div.generalOverlay.js-general-overlay.displayNone.js-promotional')))
    element = d.find_element_by_css_selector('body > div.generalOverlay.js-general-overlay.displayNone.js-promotional')
    d.execute_script("var element = arguments[0];element.parentNode.removeChild(element);", element)
    print("Pop-Up Negated")
except TimeoutException:
    print("No Pop-Up Detected")
aElements = d.find_elements_by_tag_name("a")
for name in aElements:
    if name.get_attribute("href") is not None and "javascript:void" in name.get_attribute("href"):
        if (name.text == i):
            print(name.text)
            name.click()
            break

Any comments on improving could would be much appreciated.

Upvotes: 1

Arjun Tandon
Arjun Tandon

Reputation: 76

Are you sure its not an Alert? One way to check is to try inspecting elements within the Alert window.

You can try either one of these:

driver.switchTo().alert().dismiss();
driver.switchTo().alert().accept();

If that throws a NoAlertPresentException,a screenshot of the popup window might help us help you better.

Upvotes: 1

Related Questions