使用watir-webdriver输入嵌套在iframe中的文本输入

时间:2021-12-15 19:36:54

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

我正在尝试使用Watir自动将文本输入到嵌套在iframe中的输入字段。

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

这就是我用来尝试访问iframe的方法

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

fr = b.frame(:index => 0)fr.text.include? '记住我的登录ID'

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

这是假的。此外,如果我尝试输出fr.text它只是一个空字符串。

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

我还创建了一个HTML的小提琴,我试图对此进行操作。

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,因为没有指定ID或类。

<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">

1 个解决方案

#1


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).

第一个问题是你正在寻找错误的框架。页面上实际上有4个框架(假设您已单击“登录”链接)。

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

要避免按索引定位元素,还应考虑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:

正如你在这个html中看到的那样,框架是“_modal_content”div标签中的第一帧。因此,您可以找到框架:

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.

第二个问题是text方法为frame元素返回一个空字符串。我不记得它是否总是这样,但似乎你需要检查框架中特定元素的文本(而不是框架本身)。最简单的方法是获取第一个元素,即html元素。

fr.element.text.include? 'Remember My Sign-In ID'
#=> true

#1


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).

第一个问题是你正在寻找错误的框架。页面上实际上有4个框架(假设您已单击“登录”链接)。

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

要避免按索引定位元素,还应考虑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:

正如你在这个html中看到的那样,框架是“_modal_content”div标签中的第一帧。因此,您可以找到框架:

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.

第二个问题是text方法为frame元素返回一个空字符串。我不记得它是否总是这样,但似乎你需要检查框架中特定元素的文本(而不是框架本身)。最简单的方法是获取第一个元素,即html元素。

fr.element.text.include? 'Remember My Sign-In ID'
#=> true