【问题标题】:Playwright Python: Get Attribute inside Iframe剧作家 Python:在 Iframe 中获取属性
【发布时间】:2022-11-21 13:29:44
【问题描述】:

我正在尝试使用 Playwright 和 Python 获取 iframe 元素的“src”。 这是我要访问的 HTML:

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

我的目标是获取“src”属性。 这是我到目前为止尝试过的

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

    #also 

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

以及许多其他不起作用的东西, 大多数时候我得到:

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

我应该如何处理这件事?

【问题讨论】:

    标签: python playwright playwright-python


    【解决方案1】:

    在没有看到实际站点的情况下,传统的选择和 get_attribute 应该就足够了:

    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()
    

    如果框架不是立即可见的,您可以通过将 src = 行替换为等待它

    src = (
        page.wait_for_selector('iframe[title="IFRAME_NAME"]')
            .get_attribute("src")
    )
    

    【讨论】:

      猜你喜欢
      • 2022-12-22
      • 2022-11-01
      • 2021-07-20
      • 1970-01-01
      • 1970-01-01
      • 2021-07-05
      • 2022-07-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多