I’m trying to remove a pop-up inside an iframe that is inside a div which is inside an overlay div. I have to find the close button and click. My code is able to locate the iframe and is printing the iframe before switching to it and is shown empty. It is the same iframe that contains the pop up. I’m getting error while switching to it.
iframe:
<div id="sp_message_container_964490" style="display: block;">
<iframe src="https://cdn.privacy-mgmt.com/index.html
message_id=
964490&consentUUID=undefined&
preload_message=true&hasCsp=true&version=v1&
consent_origin=https%3A%2F%2Fcdn.privacy-
mgmt.com%2Fconsent%2Ftcfv2&mms_origin=https%3A%2F%2Fcdn.privacy-
mgmt.com%2Fmms%2Fv2&
consentLanguage=en" id="sp_message_iframe_964490" title="SP Consent Message">...
</iframe>
</div>
inside iframe:
<!DOCTYPE html><html lang="en"><head>
<title>Notice Message App</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-
scale=1.0,user-scalable=no,viewport-fit=cover">
...............
<button title="Essential cookies only" aria-label="Essential cookies only"
class="message-component message-button no-children focusable sp_choice_type_13"
style="opacity: 1; padding: 10px 20px; margin: 10px; border-width: 1px; border-color:
rgb(49, 87, 161); border-radius: 4px; border-style: solid; font-size: 14px; font-
weight: 700; color: rgb(255, 255, 255);
font-family: "trebuchet ms", helvetica, sans-serif; width: auto; background:
rgb(49, 87, 161);">Essential cookies only</button>
................
}</style></body></html>
I have to access this button within the html.
My code:
divs = driver.find_elements(By.TAG_NAME, “div”)
for div in divs:
if “message” in div.get_attribute(“id”):
iframe = div.find_element(By.TAG_NAME, “iframe”)
# for iframe in iframes:
try:
print(“beofre switching to iframe”, iframe.get_attribute(“outerHTML”))
driver.switch_to.frame(iframe)
print(“switched to iframe”, iframe.get_attribute(“outerHTML”))
content = driver.page_source
if ‘consent’ in content.lower():
handle_popups(driver)
return True
except Exception as e:
print(f”error switching to iframe {e}”)
driver.switch_to.default_content()
return False
How can I switch to the iframe and why is it printed empty?