-1

I want to Click on a Website button that are already going on. For example: like url: www.google.com.. I want to Click Google Search button programmatically by using any method in PHP, Javascript, Jquery and Ajax. if Anyone know Solution. Please tell my and provide the source code.

Note: We dn't need to create own button we want to click on a website button by using class and id.

I want to try this..look like this but not success..I want to click Learn HTML button that are show on iframe...in w3school website..

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<iframe src="http://www.w3schools.com" width="800" height="400"></iframe>

<script>
//setInterval(function () {$("#hand_1151079882").click();}, 3000);

function clime(){
setInterval(function () {document.getElementById("#w3-btn").click();}, 3000);
alert();
}
//using javascript 
$(document).ready(function () {
$(".cli").on('click', function(event){
    $("#w3-btn").trigger('click');
});

});

</script>
<input type="button" class="cli" name="clime" value="submit" onclick="clime()">
sonu
  • 31
  • 1
  • 9

3 Answers3

1

you need set id for iframe

var iframe = document.getElementById('w3schools-home');
var innerDoc = iframe.contentDocument || iframe.contentWindow.document;
innerDoc.getElementsByClassName('w3-btn')[0].click();

[0] is for button Learn HTML, there 19 element have class w3-btn

Tested and Work on w3schools Try it Yourself »

IMPORTANT: Make sure that the iframe is on the same domain, otherwise you can't get access to its internals. That would be cross-site scripting.


as Requested, here example PHP for get Element and Recreated to your own sites.

<?php
$url='http://www.w3schools.com/css/default.asp'; // or use $_GET['url']
$scheme=parse_url($url)['scheme'];
$host=parse_url($url)['host'];
$domain=$scheme.'://'.$host;
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_HEADER, 0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_TIMEOUT,30);
curl_setopt($ch,CURLOPT_POST, 0);
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML(substr(curl_exec($ch), curl_getinfo($ch, CURLINFO_HEADER_SIZE)));

$xpath = new DOMXpath($dom);
$aElement = $xpath->query('//a');

$length = $aElement->length;
$links=array();
for ($i = 0; $i < $length; $i++) {
    $element = $aElement->item($i);
    if ($element->tagName=='a' && trim($element->textContent)<>'') {
        foreach ($element->attributes as $attr) {
            $attrName = $attr->nodeName;
            $attrVal = $attr->nodeValue;
            if($attrName=='href' && str_replace(';','',$attrVal)<>'javascript:void(0)'){
                if(substr($attrVal, 0, 1)=='/'){
                    $links[trim($element->textContent)]=$domain.$attrVal;
                }else if(substr($attrVal, 0, 4)=='http'){
                    $links[trim($element->textContent)]=$attrVal;
                }
            }
        }
    }
}

foreach ($links as $key=>$value) {
    echo '<a href="'.$value.'">'.$key.'</a> | ';
}

You can create a javascript function to get URL from iframe and than request AJAX to PHP that have script ABOVE, you can use echo(but need foreach as example from my script) or json_encode to array $links, then reCREATED button from other url/website to your sites.

or just echo to your sites.

My script still need improvement to handle value of attribut href that use '../' or 'foldername/'

just reminder, for get access ELEMENTS on iframe that pointing to different domain is impossible.

Maunklana
  • 23
  • 1
  • 5
  • Read the my question Again. i want to click on w3schools button. I dn't have any domain access. I want to simply include the website in iframe and want to click on website button. – sonu Oct 13 '16 at 16:53
  • 1
    read the answer, I think it is totally correct, he gets the iframe, then accesses the button inside the iframe (which is what you are NOT doing and why your code is NOT working). Read the answer properly – E.Serra Oct 14 '16 at 10:58
  • i don't know what you want, but accessing other website using iframe or ajax is impossible without Cross-origin resource sharing (CORS) if you want access the sites and get the elements, you can use CURL and DOM function on PHP, mmm... its hard to explain in English. – Maunklana Oct 15 '16 at 05:24
  • Ya... I want to other site access. I want to click other website button include in our page with iframe. It's Possible. My all Code and your code are not working. If You think that your code are working then provide me complete example using my above code and click on iframe website button. And If it's possible please provide me complete source code. You can use any website in iframe. And click on any button with our code. please help asap... – sonu Oct 15 '16 at 12:19
  • your need is script that auto click to other sites. can i know this script just only for you/your internal company? if right you can use tampermonkey to inject script(javascript) to website that you access from your browser. if this for login/register to other sites programatically is kinda hard if using PHP. you can develop from my php script above. but i not guarantee that work. – Maunklana Oct 17 '16 at 10:20
0

You seem to be missing the element with the id w3-btn which gets clicked programmatically.

Also, it seems both of these are doing the same thing ...

function clime(){
setInterval(function () {document.getElementById("#w3-btn").click();}, 3000);
alert();
}

and

$(document).ready(function () {
$(".cli").on('click', function(event){
    $("#w3-btn").trigger('click');
});

});

both are trying to click an element with id w3-btn, just in different ways. You only need one of them.

blr
  • 908
  • 4
  • 8
  • Thanks for Suggestion. But My button id is right. I think you need to try my code. I want to click on a website button that are already going on another server. Including the website in our page using iframe and want to click on website button. Like: www.google.com, w3schools and etc... – sonu Oct 13 '16 at 11:47
0

This might help:

<script>
jQuery(function(){
   jQuery('#modal').click();
});
</script>

I got this answer from here.

Community
  • 1
  • 1
Sahil Lakhwani
  • 108
  • 2
  • 11
  • Dear I think you want to review the my question. I want to click on a website button that are already going on another server. Including the website in our page using iframe and want to click on website button. Like: www.google.com, w3schools and etc... – sonu Oct 13 '16 at 11:45