0

I'm trying to get 'src' of iframe element using Playwright and Python. Here is the HTML I'm trying to access:

<iframe title="IFRAME_NAME" src="https://www.data_I_want_TO_get.com"> </iframe>

my goal is to grab 'src' attribute. here is what I've tried so far

    src=page.frame_locator("IFRAME_NAME")
    print(src.inner_html())

    #also 

    src=page.frame_locator("IFRAME_NAME").get_by_role("src")
    print(src)

and many other things that are NOT working, most of the time I get:

AttributeError: 'FrameLocator' object has no attribute 'inner_html'
nor .get_attribute

How should I proceed about this?

WillX0r
  • 53
  • 6

1 Answers1

2

Absent seeing the actual site, a traditional selection and get_attribute should be sufficient:

from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch(headless=True)
    page = browser.new_page()
    page.set_content("""
    <iframe title="IFRAME_NAME" src="https://www.data_I_want_TO_get.com"></iframe>
    """)
    src = page.get_attribute('iframe[title="IFRAME_NAME"]', "src")
    print(src)  # => https://www.data_I_want_TO_get.com
    browser.close()

If the frame isn't immediately visible you can wait for it by replacing the src = line with

src = (
    page.wait_for_selector('iframe[title="IFRAME_NAME"]')
        .get_attribute("src")
)
ggorlen
  • 44,755
  • 7
  • 76
  • 106