I'm trying to automatically enter text to an input field that is nested within an iframe using Watir.

This is what I'm using to try and access the iframe

fr = b.frame(:index => 0) fr.text.include? 'Remember My Sign-In ID'

This is returning false. Also if I try to output fr.text it is just an empty string.

I've also created a fiddle of the HTML I'm trying to run this against.

Fiddle

Or you can hit the actual site here.

I'm trying to access the iframe by index because there is no ID or classes specified.

<iframe height="auto" frameborder="0" src="https://auth.api.sonyentertainmentnetwork.com/2.0/oauth/auth…rtainmentnetwork.com/shared/html/signinRedirect_SSOAuth.html" marginwidth="0" marginheight="0">

有帮助吗?

解决方案

Locating the right frame

The first problem is that you are looking in the wrong frame. There are actually 4 frames on the page (assuming you have clicked the Sign In link).

To avoid locating the element by index, you should also consider the attributes of elements around iframe:

<div class="_modal_content close-this-modal" id="_modal_content" tabindex="998">
  <div class="modal modal-iframe" tabindex="1000">
    <h1></h1>
    <div style="height: 638px;" class="modal-iframe-wrapper">
      <iframe marginheight="0" marginwidth="0" src="removed.html" frameborder="0" height="auto">

As you can seen in this html, the frame is the first frame in the "_modal_content" div tag. Therefore, you can find the frame with:

fr = b.div(:id => '_modal_content').frame

Getting text of a frame

The second problem is that the text method returns a blank string for frame elements. I do not remember if it has always been like this, but it seems that you need to check the text of a specific element in the frame (rather than the frame itself). The easiest way to do this is to get the first element, which is the html element.

fr.element.text.include? 'Remember My Sign-In ID'
#=> true
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top