无法访问 iframe 中的文本字段 [英] Unable to access text field in an iframe

查看:39
本文介绍了无法访问 iframe 中的文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Watir-Webdriver 自动化测试脚本.在编写脚本时,我必须填写登录详细信息.现在它会在一个简单的 text_field 中,我知道语法并且会更容易.但是当我检查该页面上的元素时,我看到以下内容:

所以,正如我所看到的,电子邮件字段位于表格 -> 单元格内.所以我无法使用 Watir 访问它.我是这样试的:

b.text_field(:id,'lid').set 'my value'

有人可以帮忙吗?

解决方案

在下一行中,Watir 将在 iframes(和frames)之外的任何位置查找元素.

b.text_field(:id,'lid').set 'my value'

与其他元素不同,当元素在框架中时,您必须告诉 Watir.这与您将元素的搜索范围限定到特定元素的方式类似.

例如,如果只有1个iframe或者你的元素在第一个iframe中,你可以这样做(注意添加了.iframe):

b.iframe.text_field(:id => 'lid').set 'my value'

如果有多个 iframe,您需要添加参数以更具体地说明要使用哪个 iframe.例如,框架有一个 id:

b.iframe(:id => 'zohoiam').text_field(:id,'lid').set 'my value'

I am trying to automate script for testing using Watir-Webdriver. While writing a script, I am at a point where I have to fill in log in details. Now would it have been in a simple text_field, I know the syntax and it would be easier. But when I inspect elements on that page, I see following:

<div class="signin-part">

<iframe id="zohoiam" frameborder="0" allowtransparency="true" style="width:430px;height:350px;margin: 0;float:left" src="https://accounts.zoho.com/login?servicename=ZohoCRM&serviceu…o&hide_signup=true&css=https://www.zoho.com/css/prd-sign.css" scrolling="no" name="zohoiam" marginheight="0" marginwidth="0">
    #document
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1.dtd">
        <html>
            <head></head>
            <body class="bodycolor" onload="setlwh();">
                <noscript></noscript>
                <div id="enableCookie" class="loginNotes" style="display:none;"></div>
                <table id="outertable" class="mobile-login" width="100%" style="">
                    <tbody>
                        <tr valign="top"></tr>
                        <tr></tr>
                        <tr>
                            <td align="center">
                                <form id="msgOrgUserform" class="hide" method="post" name="msgOrgUserform"></form>
                                <div id="loginform">
                                    <div class="mobilelogo"></div>
                                    <form id="login" class="" novalidate="" method="post" onsubmit="javascript:return submitlogin(this);" name="login">
                                        <table id="inntbl" class="mob_width" cellspacing="0" cellpadding="0" align="center">
                                            <tbody>
                                                <tr>
                                                    <td valign="top">
                                                        <table class="mob_width" width="260" cellspacing="2" cellpadding="1" align="center">
                                                            <tbody>
                                                                <tr></tr>
                                                                <tr></tr>
                                                                <tr>
                                                                    <td class="label"></td>
                                                                    <td align="left">
                                                                        <input id="lid" class="input usrbx" type="email" onkeypress="clearmsg()" value="" name="lid"></input>
                                                                    </td>
                                                                </tr>

So, as I can see, Email field is located inside table -> cell. So I am not able to access it using Watir. I tried it like this:

b.text_field(:id,'lid').set 'my value'

Can someone help?

解决方案

In the following line, Watir will look for the element anywhere except for in iframes (and frames).

b.text_field(:id,'lid').set 'my value'

Unlike other elements, you must tell Watir when an element is in a frame. This is done similar to how you would scope the search of an element to a specific element.

For example, if there is only 1 iframe or your element is in the first iframe, you can do (noting the addition of the .iframe):

b.iframe.text_field(:id => 'lid').set 'my value'

If there are multiple iframes, you will need to add parameters to be more specific about which iframe to use. For example, the frame has an id:

b.iframe(:id => 'zohoiam').text_field(:id,'lid').set 'my value'

这篇关于无法访问 iframe 中的文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆