I tried to get the contents of an iframe in a page using Selenium / BeautifulSoup, but I can't.
If you get all the HTML with BeautifulSoup, the result will be as follows.
Code to get HTML
BeautifulSoup(driver.page_source, 'html.parser')
Acquisition result
<html>
    <head>
        <!-- head -->
    </head>
    <body>
        <!-- body -->
        <iframe id="iframe" 
                scrolling="yes"
                src="https://example.com/iframe"
                src_data="https://example.com/iframe">
        </iframe>
    </body>
</html>
I want to see and operate in the iframe.
In the code below, the driver focuses on the iframe.
iframe = driver.find_element_by_id('#iframe')
driver.switch_to.frame(iframe)
Code to get HTML
BeautifulSoup(driver.page_source, 'html.parser')
Make sure the HTML is an iframe in.
https://www.selenium.dev/documentation/ja/webdriver/browser_manipulation/#webelementを使う
To return to the original window, use the following code.
driver.switch_to.default_content()
Recommended Posts