1

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?

1 Answer 1

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")
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.