0

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: &quot;Open Sans&quot;, 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?

cybertextron
  • 10,547
  • 28
  • 104
  • 208

2 Answers2

2

You can try the below options.

option 1: click on the body element before entering the text/cleaning the text.

b.click()
# use javascript click(), if normal click not worked
driver.execute_script("arguments[0].click()",b)

option 2: Trigger the on-change or associated event, after entering the data in the body.

driver.execute_script("arguments[0].dispatchEvent(new Event('onchange', {'bubbles': true,'cancelable': true}));",b)

You can try using the both options individually or combined.

Refer to this post to know the events associated to the body element.

supputuri
  • 13,644
  • 2
  • 21
  • 39
0

frame_to_be_available_and_switch_to_it()

frame_to_be_available_and_switch_to_it() expectation for checking whether the given frame is available to switch to. If the frame is available it switches the given driver to the specified frame. This method doesn't returns anything. So irrespective of success/failure status of the execution of the following line of code :

iframes = wait.until(EC.frame_to_be_available_and_switch_to_it((By.CLASS_NAME, "wysihtml5-sandbox")))

iframes will be always NULL and the code block within if iframes: won't be executed.

Possibly your manual intervention is helping the driver to get the focus within the desired element and hence types the text. You need to make a small change and your effective code block will be:

wait.until(EC.frame_to_be_available_and_switch_to_it((By.CLASS_NAME, "wysihtml5-sandbox")))
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)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352