Search code examples
pythonseleniumselenium-webdriversplinter

Use splinter to write to an html body


I've found out from the page source that the forum writes into something like this inside of an iframe that isn't named:

<iframe style="height: 360px;">
    <html>
        <body>
        Forum text goes here.
        </body>
    </html>
</iframe>

I tried getting the ID using :

browser.execute_script("$('body').text('forum text')")

I think that it could be done in javascript with something like this, but I'm not sure how to find the element in question because it has no identifier, and simple browser.fill('body', text) calls doesn't work because it's not marked as an input.

The forum in question is hosted on proboards, if that is any help.

Edit: For anyone interested in what the final code looked like:

with browser.get_iframe(1) as iframe:
     iframe.execute_script("document.getElementsByTagName('body')[0].innerHTML = 'helloworld'")

Solution

  • You need to switch into the context of the iframe. Since it does not have id or name, simply switch to it by index. Assuming this is the first iframe on the page:

    with browser.get_iframe(0) as iframe:
        # now we are inside the iframe