I am testing around with WebBrowser in C# and I found a site which has got an button but the button has no ID it's a div.
<div class="pc-image-info-box-button-btn-text pc-cursor"><i class="fa fa-heart" aria-hidden="true"></i>GO</div>
Is it possible to tell the WebBrowser to click this button? When the form starts the WebBrowser naviagates to the site than the programm wait's for the WebBrowser to finish loading and once its loaded it should click on the button.
Thats my try right now:
webBrowser1.Url = new Uri(textBox2.Text);
while(webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
webBrowser1.Navigate("javascript: document.getElementsByClassName('pc-image-info-box-button-btn-text pc-cursor')[0].click();void(0);");
But it doesn't work.
Of course it works, but only if your div has an onclick event handler set.
var div = document.createElement('div')
// undefined
div.onclick = function(){ console.log(':)')}
// ƒ (){ console.log(':)')}
div.click()
// :)
Look at this example:
<html>
<head>
</head>
<body>
<h2 class="text-center">example</h2>
<div class="pc-image-info-box-button-btn-text pc-cursor" ><i class="fa fa-heart" aria-hidden="true"></i>GO</div>
</body>
<script type="text/javascript">
// attach function on document loaded event
document.addEventListener('DOMContentLoaded', function(){
// Your selector will be different most likely
var element = document.getElementsByClassName('pc-image-info-box-button-btn-text')[0];
// add click event listener to selected element
element.addEventListener("click", clickFunc);
// declare function which fires on click
function clickFunc(){ alert('clicked!'); }
// trigger element click event
element.dispatchEvent(new Event('click'));
}, false);
</script>
</html>
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.