Click here to Skip to main content
14,304,697 members
Rate this:
Please Sign up or sign in to vote.
See more:
hello guys , here a code that`s contains a button i need to click but i can`t do it although i tried many way to do it using
currentwb.Document.GetElementById

but as we see this code have no id for the element ,,
<a class="single_like_button btn3-wrap"  önclick="openWin_1083724();">
						                    <span> </span><div class="btn3">Click Here</div></a>


so any suggestion about how i can get this element is clicked ?
Posted
Updated 3-Nov-13 8:21am
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

You can't click on it? This makes no sense to me. You want to trigger the click event on the element from javascript? Try this: http://www.nonobtrusive.com/2011/11/29/programatically-fire-crossbrowser-click-event-with-javascript/[^]

[Update]
If you want to automate IE, you better use this approach: http://harness.codeplex.com/[^]

If you stick to WebBrowser control (currentwb), try this approach:

HtmlElement link = currentwb.Document.GetElementByID("id_of_element")
link.InvokeMember("Click")


Or, if you can't assign id for some reason, you can use this approach.
HtmlElementCollection links = currentwb.Document.GetElementsByTagName("A");

foreach (HtmlElement link in links)
{
    if (link.InnerText.Equals("Link text"))
        link.InvokeMember("Click");
}

Be aware to put in the IF statement the proper condition to identify your link.
   
v2
Comments
   
I would rather agree with "This makes no sense...". Using fireClick you refer to is possible, but usually not needed. Please see my answer.
—SA
Zoltán Zörgő 3-Nov-13 16:26pm
   
True. Not needed in general, and this code snippet does not require it either. But who knows what the final purpose of OP is... sounds like horse, smells like horse, looks like a horse... but might be a zebra though :)
   
This is only because Zebra is actually the horse (equus), not "like a horse" ... :-)
—SA
Ali Al-Masry 4-Nov-13 7:51am
   
how i use this Java code while i am doing that in C# ?
Zoltán Zörgő 4-Nov-13 8:02am
   
1) It was Javascript, not Java
2) See update. Note, that GetElementById needs an ID to work with, your element has no id.
Ali Al-Masry 4-Nov-13 8:24am
   
i know my element is have no id and i need a way to click it , any suggestion ?
Zoltán Zörgő 4-Nov-13 9:25am
   
Read my update, please. There you have all the two options. If the first one does not apply, you still have to distinguish that one from all the other A elements. But this part depends on the rest of the html code - and on how it changes between calls.
Ali Al-Masry 4-Nov-13 13:33pm
   
i have tried it many times with the code you provided .. but still didn't worked !!

here is the HTML script which contains button code in it without an ID


<span>
</span>
<div class="btn3">Click Here</div>
Zoltán Zörgő 4-Nov-13 14:18pm
   
Is this the only A element with classes single_like_button and btn3-wrap classes on the page? Or is it the first or third, or 27th A element, or is there any logic how you can find this element in all situations? Is it the only one that is calling openWin_1083724(); function, and it is calling this function in all cases? It looks like a generated name to me...

The key is, that you have to identify that A element somehow. If you can't than you can not solve this. But nobody else can tell you this, you have to find out, since you have access to the page you want to control. The code portion you have given is simply not enough. Please consider, that the identification logic has to be valid in all situations, for every download of the page.
Ali Al-Masry 4-Nov-13 15:31pm
   
so brother i tried to get more info about my issue and i found its the web page is using an script and its not located in page source , i got it using Fire Fox Inspect Element .. , and i still need a way to click this button !!
Zoltán Zörgő 4-Nov-13 15:41pm
   
What's your final goal anyway? Clicking programatically might not be the only option. Most likely isn't. Any web application is after all a sequence of http requests and responses. Practically all web automation tasks can be emulated this way. It looks more complicated, but it is rather an other approach. Please remember, that we can't see your screen, read your mind - might happen that you don't see the problem in the right way, or you lack of proper technical knowledge to address it.
Ali Al-Masry 5-Nov-13 7:17am
   
thanks brother for trying to help me now i used many ways to click any element with the tags i added ,, and its worked fine and clicked many elements as my code , but its still not clicking the one i am looking for ...

but after i close all opened new web browser tabs its open the another IE window but its disappears after 1-2 minutes also before its load page contents !!
Ali Al-Masry 5-Nov-13 7:25am
   
here is the code from Fire Fox Inspect Element :

<div class="page_title">
Masritna
</div><div style="width:100px; height:100px; overflow:hidden;"><img style="width:100%; height:auto;" title="Masritna" alt="Masritna" src="/img/cook.png"></img></div><span> … </span><div class="btn3">
Click Here
</div>
Ali Al-Masry 5-Nov-13 7:29am
   
and here is page source :



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="Test" />
<meta name="description" content="Get Likes, Subscribers, Followers, Views and Hits . Easy registration, 50 points at start and daily 150 bonus points for active users. Start Promotion Just Now and invite all your friends to join Example." />
<meta name="Author" content="Test" />
<meta http-equiv="imagetoolbar" content="no" />
<meta name="audience" content="all" />
<meta name="rating" content="General" />
<meta name="revisit-after" content="2 Day" />
<meta name="resource-type" content="document" />
<meta name="robots" content="All" />
<meta name="google-translate-customization" content="3f4ffc751b6f619-aaa1185795a36d3e-gc243fde537c28ad5-11"></meta>

<link rel="shortcut icon" href="/favicon.ico" />
<link href="/styles/style2.css" rel="stylesheet" type="text/css" />
<link href="/scripts/jqueryui/css/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />


<link rel="stylesheet" type="text/css" href="/scripts/msgbox/jquery.msgbox.css" />
<link rel="stylesheet" type="text/css" href="/scripts/tiptip/tipTip.css" />

<script type="text/javascript" src="/scripts/jquery.min.js"></script>
<script type="text/javascript" src="/scripts/jqueryui/js/jquery-ui.min.js"></script>

<script type="text/javascript" src="/scripts/showLoading.js" ></script>
<script type="text/javascript" src="/scripts/msgbox/jquery.msgbox.min.js"></script>
<script type="text/javascript" src="/scripts/simple_loading/simpleload.js"></script>
<!-- <script src="/scripts/bxslider/jquery.easing.1.3.js" type="text/javascript"></script>
<script src="/scripts/bxslider/jquery.bxSlider.min.js" type="text/javascript"></script> -->
<script src="/scripts/tiptip/jquery.tipTip.minified.js" type="text/javascript"></script>
<script src="/scripts/jquery.floating_popup.min.js" type="text/javascript"></script>


<link href="https://plus.google.com/104027597398828648329" rel="publisher" />

<script type="text/javascript" src="https://apis.google.com/js/plusone.js">
{parsetags: 'explicit'}
</script>


<script type="text/javascript" src="/scripts/ready.js?1383653168"></script>

<script type="text/javascript" src="/scripts/script.js?1383653168"></script>

<title>Example.com - Facebook Likes</title>
</head>



<body>


<script type="text/javascript">
(function(){
var bsa = document.createElement('script');
bsa.type = 'text/javascript';
bsa.async = true;
bsa.src = 'http://s3.buysellads.com/ac/bsa.js';
(document.getElementsByTagName('head')[0]||document.getElementsByTagName('body')[0]).appendChild(bsa);
})();
</script>


<div id="dialog-overlay"></div>
<div id="dialog-ui"></div>

<div id="Div1">
</div>

<script type="text/javascript">
(function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) { return; }
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
fjs.parentNode.insertBefore(js, fjs);
} (document, 'script', 'facebook-jssdk'));
jQuery(document).ready(function(){

});
</script>

<div class="wrapper">
<div id="headerCont">
<div class="header">
<!--header header BEGIN--><div id="dialog-overlay"></div>
<div id="dialog-box">
<div class="dialog-content">
<div id="dialog-message"></div>
</div>
</div>

<div class="header_block">



<img src="/images/layout/logo1.png" alt="Add Me

Zoltán Zörgő 5-Nov-13 7:37am
   
Is this a publicly available page? If yes, can you share the URL?
Ali Al-Masry 5-Nov-13 8:18am
   
http://addmefast.com/free_points/facebook_likes.html
Zoltán Zörgő 5-Nov-13 9:28am
   
I will try to register from home. Is it a free service?
Ali Al-Masry 5-Nov-13 9:34am
   
yes its free , and i am still trying with it !!
Zoltán Zörgő 5-Nov-13 15:08pm
   
For me, that page looks different. I no link alike the one you have posted. Sorry, I can't help you further.
Rate this:
Please Sign up or sign in to vote.

Solution 2

"Click" is something which you do with a mouse or, in fact, with a keyboard. If you think about it, you will understand that what you need is not a click. You simply want to have "the same effect as the user's click on the element". In other works, you simply need to call openWin_1083724() from your JavaScript.

—SA
   
Comments
Ali Al-Masry 4-Nov-13 7:52am
   
right , show me how with a example please .. i have no idea about how to run or add java script to C# ???
   
Example? Sure:

function clickBtn3() { openWin_1083724(); }

Only never give things such names, with numbers, etc. All names should be semantic.

Now what "java scripts"?! There is no such thing. Java is not a scripting language. If you mean JavaScript, it is something totally unrelated to Java. "Run or add JavaScript to C#"?! Look, it shows that you should first learn how 1) Web works, 2) what ASP.NET does and how it works, and so on. Right now you have no a clue. You need to learn it first, and this is not a matter of a Quick Answer.

In brief, ASP.NET works only on the server part. You use C# or other language to get HTTP request and generate HTTP response. The response can include anything (it is defined by its content type), including JavaScript. A browser on a client side receives HTTP response and processes it, in particular, it can run JavaScript. And then you need to learn how to make JavaScript working. So, you need to learn at least some JavaScript.

Are you getting the idea?

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100