I am trying to scrape data from a news website and anytime a news article is clicked on, an ad shows up. I try to switch the control to the iframe to close the ad but it keeps on throwing "NoSuchElement" exception.
Below is the iframe that I want to switch to. The id is unique to this iframe.
<iframe title="Advertisement" id="ad_iframe" name="ad_iframe" scrolling="no" src="about:blank" frameborder="0" width="556px" height="222px" style="border: 0px; vertical-align: bottom; width: 556px; height: 222px;"></iframe>
My code is as follows:
try:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(driver.find_element_by_xpath('//iframe[@id="ad_iframe"]')))
driver.find_element_by_xpath('//div[@id="dismiss-button"]').click()
except NoSuchElementException:
headline = driver.find_element_by_xpath("//section[@class='col-md-8 content']/h1")
date = driver.find_element_by_xpath("//p[@class='small']")
The close button element is there in the iframe, and looks like:
<div class="ns-g3arv-e-19 button-common close-button" id="dismiss-button" x-ns-g3arv-e="19" x-overflow-forbidden="xy" aria-label="Close ad" role="button" tabindex="0"><div class="ns-g3arv-e-20" x-ns-g3arv-e="20" x-overflow-forbidden="xy"><span class="ns-g3arv-e-21" dir="auto" x-ns-g3arv-e="21" x-score="1">Close</span></div></div>
I am unable to find what is going wrong here? Why does selenium not 'switch' to the selected iframe?