简体   繁体   中英

How to click on a <span> button inside a website using VB.NET

so I want to click on a button in a website. I tried using the GetElementById but the button has no ID and it has those span things that confuse me. It also has no href so I guess its not a link or something nor it has a "a" tag or what do they call that (very sorry i do not know how to code well yet). Here is the button's HTML code :

<span class="now-click" style="display: none;">Click</span>

I tried those but didn't help : This and This

Do you know how could I make a button in my Windows Form Application press on this button with InvokeMember("click")?

What you need might is the following 3 lines of code:

var span = document.querySelector("span.now-click");
var click = new Event('click');
span.dispatchEvent(click);

What is essentially happening here is that we are fetching the span from the DOM, and after that creating a click event. Subsequently, we are dispatching the aforesaid event on the eventTarget which is the span in this case.

Here I have made a little demo to give you a clearer understanding of the same. When the 'Tester' button is clicked, is calls a function which runs the above 3 lines of code. Also I have added an EventListener to the span which causes an alert saying 'Span was clicked' whenever the span is clicked. Hope this makes it clear. You can see that clicking the span or the button have the same affect, ie clicking the span.

 function clickSpan() { var span= document.querySelector("span.now-click"); var click= new Event('click'); span.dispatchEvent(click); } 
 <button onclick="clickSpan()">Tester</button> <span class="now-click" onclick="alert('Span was clicked.')">This is span.</span> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM