I have been trying to click a button on a website to close a pop-up(specifically Google Vignette). I will say I am a beginner. If you want to see the html, please follow the steps.
This is the website : https://www.finscreener.org/earnings/earnings-reported?o=1001&pg=1
But you will have to click the "last button" at the bottom index, since Google Vignette only pops up when you try to get out of the current website.
If you want to inspect it, then maximize the window, right click somewhere outside of the pop-up. If not, the pop up will disappear and you cannot inspect.
Anyways, when the Google ad appears I want to dismiss it. I tried the code below to change the iframe to the advertisement,
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'ad_iframe')))
iframe = driver.find_element(By.XPATH,"//iframe[@name='ad_iframe']")
driver.switch_to.frame(iframe)
But the code cannot locate the iframe and gives me a TimeoutException for the WebDriverWait line.
I have tried the following, but the code absolutely refuses to locate the advertisement iframe.
close_button = driver.find_element(By.XPATH, "/html/body/div[2]/div[2]/div[2]/div/div/div[2]/div/div/div[3]").click()
How can i locate the iframe and switch to it?
You have an iframe
inside another one. You will have to switch two frames to reach the close button.
Solution 1: Close the ad
with webdriver.Chrome() as driver:
driver.get(url)
wait = WebDriverWait(driver, 10)
# Wait until the frame appears and switch to it
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, 'aswift_5')))
driver.switch_to.frame('ad_iframe')
driver.find_element(By.ID, 'dismiss-button').click()
You also must include the following imports:
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
Solution 2: Refresh the page
In you case this might be an easier solution, by refreshing the page it will also "dismiss" the popover.
driver.refresh()