I'm currently scraping a page formatted as such:
<div id="container>
<script>Script that cause iframe contents to load correctly</script>
<iframe>Contents of iFrame</iframe>
<script>More scripts</script>
</div>
I can scrape the page easily, but this does not scrape the iframe contents, and so I switched frames with:
driver.switch_to.frame(iframeElement)
And this allows me to get the iframe contents. This leads me to my question now of how I can take the container div, and insert the contents of the scraped iframe within the scraped div. The way the page is setup, there are dynamic scripts just before the iframe that allow for the contents of the iframe to work which is why I need to embed the iframe contents within the scraped div.
Relevant Python below:
driver.get(url)
iframeElement = driver.find_element_by_tag_name('iframe')
driver.switch_to.frame(iframeElement)
time.sleep(3) #Wait for the contents to generate
# driver.switch_to_default_content() #Commented out, but I know to use this to exit out of the iframe
html = driver.page_source
soup=BeautifulSoup(html, features="lxml")
print(soup)
print(soup.find("div", {"id": "Container"})) #Let's see the HTML of the container
soupStr=str(soup)
Con = str(soup.find("div", {"id": "Container"})) #Create a variable with JUST the container HTML
with open('iframeWithinDiv.html', 'w', encoding='utf-8') as f_out: #Save the file
f_out.write(soupStr)```