Unable to access text field in an iframe

我与影子孤独终老i 提交于 2019-12-01 13:35:46

问题


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?


回答1:


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'


来源:https://stackoverflow.com/questions/28201554/unable-to-access-text-field-in-an-iframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!