How to switch iframes InternJS

2019-06-01 09:36发布

问题:

I need to switch to an iframe that has a dynamic name and id.

<iframe name="easyXDM_1435765828615" id="easyXDM_1435765828615">...</iframe>

I noticed LeadFoot has a switchToFrame() function, but when I pass it an element that was returned from find() I get the error "MoveTargetOutOfBounds: POST http.../moveto / {"element":"16"} Offset within element cannot be scrolled into view".

Is there a better way to do this using execute() and some javascript, or how can I make switchToFrame() work?

回答1:

Depending on how your iframes are setup on the page you could try something like this:

    .switchToFrame(null)
    .findAllByClassName('iframe')
    .then(
      function(iframes) {
         new remote.constructor(remote.session)
        .switchToFrame(iframes[iframes.length-1])
      }
    )

This assumes that the iframe you want to switch to is last to be found, which is obviously a big assumption. You might be able to tweak this though, if the dynamically created iframes appear in the same order every time.