Get value of input field inside an iframe
If the framed site is on a different domain, you cannot directly read the value from the remote iframe. Even so, the answer is yes: It is still possible to get that value, by proxying the framed site through your own domain.
For example, in an HTML page on my site I have an iFrame whose contents are sourced from another website. The iFrame content is a single select field.
I need to be able to read the selected value on my site. In other words, I need to use the select list from another domain inside my own application. I do not have control over any server settings.
Initially therefore we might be tempted to do something like this (simplified):
HTML in my site:
<iframe name='select_frame' src='http://www.othersite.com/select.php?initial_name=jim'></iframe>
<input type='button' name='save' value='SAVE'>
HTML contents of iFrame (loaded from select.php
on another domain):
<select id='select_name'>
<option value='john'>John</option>
<option value='jim' selected>Jim</option>
</select>
jQuery:
$('input:button[name=save]').click(function() {
var name = $('iframe[name=select_frame]').contents().find('#select_name').val();
});
However, I receive this javascript error when I attempt to read the value:
Blocked a frame with origin "http://www.myownsite.com" from accessing a frame with origin "http://www.othersite.com". Protocols, domains, and ports must match.
To get around this problem, you can proxy the content at "http://www.othersite.com", hosting it instead on "http://www.myownsite.com". For example, if you are using PHP, have a PHP script that reads the contents from the other site using a method like file_get_contents()
or curl
etc.
So, create a script (for example: select_local.php
in the current directory) on your own site with contents similar to this:
PHP content of select_local.php:
<?php
$url = "http://www.othersite.com/select.php?" . $_SERVER['QUERY_STRING'];
$html_select = file_get_contents($url);
echo $html_select;
?>
Also modify the HTML to call this local (instead of the remote) script:
<iframe name='select_frame' src='select_local.php?initial_name=jim'></iframe>
<input type='button' name='save' value='SAVE'>
Now your browser should think that it is loading the iFrame content from the same domain.
Unable to access text field in an iframe
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'
How would I change the src of an iframe with a text input?
Change the existing iframe source:
function changeIframe(url) {
document.getElementById('change').src = url;
}
Use the onchange
event instead of onsubmit
:
<input type="text" id="minecraftInput" onchange="changeIframe(this.value)" id="detroit" placeholder="https://classic.minecraft.net/?join=somelettersand#s" name="link" required>
how to writing to text box inside an iframe
You can' do that because that would be cross-site scripting, which is prohibited for security reasons. You could use a proxy, feed the HTML in the iframe
through your own site so it's no longer cross-site from the browser's perspective.
If the <iframe>
is from the same domain, the elements are easily accessible as:
$("#iFrame").contents().find("#someDiv")
Test: http://jsfiddle.net/Yy6tu/ press Ctrl+Shift+j
and see what you got!
UPDATE: In case you have the a other.html
file on your same folder, you can do this:
main.html:
<head>
$('document').ready(function(){
$("#if").contents().find("#b").val('lalalala');
});
</head>
<body>
<input type='button' name='executer' value="Maro">
<iframe width="600" height="400" src='other.html' name='if' id="if"></iframe>
</body>
other.html:
<form action="x" name="form2" id="form2">
<input type="text" name="tb" id="b">
</form>
That should work!
Related Topics
Bootstrap Modal in Ruby on Rails Not Working
Http.Post_Form in Ruby with Custom Headers
Use [].Replace to Make a Copy of an Array
How to Ignore File Types in a Web Crawler
No Such File or Directory @ Rb_Sysopen for External Url/Rails 6.11/Ruby 3
How to Override the System Timezone in Ruby
Ternary Expression with "Defined" Returns "Expression" Instead of Value
Error While Installing Iconv on Windows by Ruby2.0.0
Using the Right Exception Subclass in Ruby
Why Does Ruby '**' Operator Have Higher Precedence Than Unary '-'
How to Create Automatically a Instance of Every Class in a Directory
How to Serialize a Ruby Digest::Sha1 Instance Object
"Bad Ecpoint" Ssl Error on Fresh Rvm Ruby 1.9.3 Install on Osx Mountain Lion
How to Access Text Field in an Iframe
Why Does Ruby Use Its Own Syntax for Safe Navigation Operator
When Is the Do Keyword Required in Ruby