I have an iframe
embedded into my webpage, and I'm using Selenium to automate tests:
<iframe class="wysihtml5-sandbox" security="restricted" allowtransparency="true" frameborder="0" width="0" height="0" marginwidth="0" marginheight="0" style="display: block; background-color: rgb(255, 255, 255); border-collapse: separate; border-color: rgb(163, 192, 216); border-style: solid; border-width: 1px; clear: none; float: none; margin: 0px; outline: rgb(0, 0, 0) none 0px; outline-offset: 0px; padding: 6px 12px; position: static; top: auto; left: auto; right: auto; bottom: auto; z-index: auto; vertical-align: baseline; text-align: start; box-shadow: rgba(0, 0, 0, 0.043) 0px 0.595067px 0.595067px 0px inset; border-radius: 4px; width: 100%; height: 434px;"></iframe>
Inside the iframe
there's a body:
<body marginwidth="0" marginheight="0" contenteditable="true" class="form-control ng-pristine ng-untouched ng-valid ng-isolate-scope ng-empty wysihtml5-editor placeholder" spellcheck="true" style="background-color: rgb(255, 255, 255); color: black; cursor: text; font-family: "Open Sans", sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: 400; line-height: 17.1429px; letter-spacing: normal; text-align: start; text-decoration: none solid rgb(0, 0, 0); text-indent: 0px; text-rendering: auto; word-break: normal; overflow-wrap: break-word; word-spacing: 0px;">Enter text ...</body>
I wrote the following Selenium automation to input text into that body, which actually displays as a text box:
iframes = wait.until(EC.frame_to_be_available_and_switch_to_it((By.CLASS_NAME, "wysihtml5-sandbox")))
if iframes:
try:
body = browser.find_elements_by_tag_name("body")
for b in body:
b.clear()
b.send_keys(text)
time.sleep(2)
# switch back to the original content
browser.switch_to.default_content()
except Exception as e:
print(e)
The issue is whenever I enter a text ("foobar") and save, the text is not saved. I noticed if I click in the input box which the body represents, then the text
is saved, otherwise it won't happen. I guess whomever wrote the code for this page expected the user to click and type in the input box (body
) in order to save when the iframe
is closed.
How can I emulate the same effect of clicking in the iframe
input box in order to fix this bug in my automation?