Ash_stack
Ash_stack

Reputation: 15

Trying to programatically click a button on a web page with cefsharp

I'm using cefsharp and vb.net to put some code together to move to the next page of this site:

https://www.recommendedagencies.com/search#{}

The idea is to read the list of company names on each page and store to a csv file. This part I can do.

The problem I have is that I can't find the name of the 'Next' button - presumably if I had that I could execute some javascript on the browser to press the button.

I've inspected the page in Firefox, but can't see any name I can use - I'm not really familiar enough with html/page design to know why not or how it works.

Could anyone tell me a good method to get button names from a web page? - I've done some searching and even asked a similar question myself before, but I can't find anything which helps, given my patchy knowledge.

Thanks

Upvotes: 0

Views: 501

Answers (1)

David Kerr
David Kerr

Reputation: 1123

  1. Inspect the DOM for the 'Next' button.
  2. Look for id's or classes that you can use to identify it.
  3. use document.querySelector() to find the element by the css selector
  4. call the element.click() function to programatically press next

const nextButton = document.querySelector('.sp-pages-nav_next')

nextButton.addEventListener('click', (event) => {
    console.log('something clicked next')
})

nextButton.click()
<div class="sp-pages-nav sp-pages-nav_next" data-reactid=".1.3.4.1">Next</div>

In the above snippet on load you can see the code nextButton.click() invokes the console log. You can click the word Next manually to the same effect.

in cefsharp perhaps something like:

browser.ExecuteJavaScriptAsync("(function(){ document.querySelector('.sp-pages-nav_next').click(); })();");

A very similar example can be found here :

https://github.com/cefsharp/CefSharp/wiki/General-Usage#1-how-do-you-call-a-javascript-method-from-net

// When executing multiple statements, group them together in an IIFE
// https://developer.mozilla.org/en-US/docs/Glossary/IIFE
// For Google.com pre-populate the search text box and click the search button
browser.ExecuteJavaScriptAsync("(function(){ document.getElementsByName('q')[0].value = 'CefSharp Was Here!'; document.getElementsByName('btnK')[0].click(); })();");

Upvotes: 1

Related Questions