python and selenium trows exception on get_attribute on iframe

Question:

As I want to test some cookies accept code, i get an exception while working with get_attribute..

Here is my Code so far:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager
from time import sleep
import random
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

chrome_options = webdriver.ChromeOptions()
    chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
chrome_options.add_experimental_option("detach", True)

driver = webdriver.Chrome(options=chrome_options)
driver.get("https://www.golem.de")
sleep(random.randrange(5,7))

print(f"URL: {driver.current_url}")

iframes = driver.find_elements(By.TAG_NAME, "iframe")

for iframe in iframes:
    driver.switch_to.frame(iframe)
    print(f"URL: {driver.current_url}")
    
    print(dir(iframe))
    iframe_attributes = iframe.get_attribute('name')
    #print(dir(iframe_attributes)
    try:
        button = driver.find_element(By.XPATH, "//button[text()='Zustimmen und weiter']")
        button.click()
        break
    except:
        driver.switch_to.default_content()

This is console output:

URL: https://www.golem.de/sonstiges/zustimmung/auswahl.html?from=https%3A%2F%2Fwww.golem.de%2F
URL: https://www.golem.de/sonstiges/zustimmung/auswahl.html?from=https%3A%2F%2Fwww.golem.de%2F
['__abstractmethods__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_abc_impl', '_execute', '_id', '_parent', '_upload', 'accessible_name', 'aria_role', 'clear', 'click', 'find_element', 'find_elements', 'get_attribute', 'get_dom_attribute', 'get_property', 'id', 'is_displayed', 'is_enabled', 'is_selected', 'location', 'location_once_scrolled_into_view', 'parent', 'rect', 'screenshot', 'screenshot_as_base64', 'screenshot_as_png', 'send_keys', 'shadow_root', 'size', 'submit', 'tag_name', 'text', 'value_of_css_property']
Traceback (most recent call last):
  File "C:DATAtest_code.py", line 31, in <module>
    iframe_attributes = iframe.get_attribute('name')

It seems that iframe has the get_attribute method, but throws nevertheless an error, when using the get_attribute method on it.
Can you help me?

PS: The reason the code is, to accept the cookies. Without the iframe loop i could not access the button, thatswhy the loop into the iframe.
Thanks in advance.

Asked By: Malta

||

Answers:

No need to loop through the iframes.you can uniquely identify the iframe with following css selector.

Use WebDriverWait() to handle dynamic page elements. This will look for a frame and switch and then look for button element and click.

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='SP Consent Message']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Zustimmen und weiter']"))).click() 

Attached the screenshot.

enter image description here

Answered By: KunduK