How to programmatically click on a div in a web browser?

I tried it for several days and still have not been successful in programmatically clicking on this div. All other input fields and buttons work fine using InvokeMember ("click") and RaiseEvent ("onclick"), but I cannot click on the following div:

<div class="pump request"> onclick="$(this).push('kjhzsd94vibjktj584ed01', null, event)" </div>

This div is repeated several times on the page, but I just want to click on the first occurrence.

This is what I have done so far:

 HtmlElementCollection c1 = wbc1.document.GetElementsByTagName("div");

 foreach (HtmlElement e2 in c1)
 {
    if (e2.GetAttribute("class").Contains("pump request"))//also this condition is not returning true
       {
          e2.RaiseEvent("onclick");              
       }
 }

@bleepzter

what if "somecontrol" is a div class instead of a div id?

since in my case I have a div class “pump request”, therefore (if I write “pump request” as somecontrol in the above example), it will return Null to me in someDiv

<div class="pump request"> onclick="$(this).push('kjhzsd94vibjktj584ed01', null, event)" </div>

@Cameron

yep I entered break ;, but the problem is that the if condition never returns true, therefore

    HtmlElementCollection c1 = wbc1.document.GetElementsByTagName("div");
    foreach (HtmlElement e2 in c1)
 {
    if (e2.GetAttribute("class").Contains("pump request"))//--> This condition is not returning true
       {
          e2.RaiseEvent("onclick"); 
          break;              
       }
 }

@ Ilya Kogan

, e2.GetAttribute( "" ), , , div ( ), : -o

+3
3

if (e2.GetAttribute("className").Contains("pump request"))
{
    e2.InvokeMember("Click");              
}
+4

, -.

 // browser is the web browser control
 HtmlElementCollection col = browser.Document.GetElementsByTagName("div");
            foreach (HtmlElement helemnt in col)
            {
                if (helemnt.InnerText !=null && helemnt.InnerText=="something") 
                {

                    helemnt.InvokeMember("Click");

                       break; // break the loop

                }



            }
+2

It's simple. Here is an example that assumes your browser control is called browser, and the div you are looking for is called somecontrol(i.e. div id somecontrol):

HtmlElement someDiv = browser.Document.All["somecontrol"];
object someDivElement = someDiv.DomElement;

MethodInfo clickMethod = someDivElement.GetType().GetMethod("click");
clickMethod.Invoke(someDivElement, null);

All this is possible through reflection.

0
source

Source: https://habr.com/ru/post/1790029/


All Articles