0

I want to get the questions and answers from People Also Ask section of google using javascript (but inside a vb.net cefsharp browser using executequery). The section needs to be clicked to be expanded and then I want both questions and answers from it.

The HTML code of the question part is something like this :

<div jsname="tJHJj" class="dnXCYb" aria-controls="_mtM8ZYO5IueaseMPheiNoAM_34" aria-expanded="false" role="button" tabindex="0" jsaction="AWEk5c">
    <div jsname="lN6iy" class="JlqpRe"><span class="JCzEY ZwRhJd"><span class="CSkcDe">Does Semrush provide free certification?</span></span>
    </div>
    <div jsname="Q8Kwad" class="aj35ze"></div>
    <div jsname="pcRaIe" class="L3Ezfd" data-ved="2ahUKEwiD7sSetpiCAxVnTWwGHQV0AzQQuk56BAgzEAI"></div>
    <div jsname="gwzXIc" class="ru2Kjc" data-ved="2ahUKEwiD7sSetpiCAxVnTWwGHQV0AzQQ36YDegQIMxAD"></div>
</div>

I want to click the above section so the section expands and then copy the answer inside it, but Iam unable to open it.

I have tried the following ways :

ChromiumWebBrowser1.ExecuteScriptAsync("document.getElementsByName('q')[0].value='semrush';document.getElementsByName('q')[0].focus();document.getElementsByName('q')[0].click();")

ChromiumWebBrowser1.ExecuteScriptAsync("document.getElementsByTagName('span')[0].click();")

'ChromiumWebBrowser1.EvaluateScriptAsync("document.querySelectorAll(""div[aria-expanded=""false""])"").forEach(button=>button.click())")

'ChromiumWebBrowser1.ExecuteScriptAsync("(function(){ document.querySelector('.div[aria-expanded=""false""])').click(); })();")

Dim qry As String = "$(document).ready(function(){
  $(""[aria-expanded]"").click(); });"
ChromiumWebBrowser1.ExecuteScriptAsync(qry)

The last 4 commands just don't work. The first two work nicely and I can also click on various other buttons but it's just the sections mentioned which I cannot make JavaSscript click the required section. I think the mistake is with the way I am trying to get to it.

Any advice would be very helpful.

3
  • I tried to improve the formatting of the question, but it may need more adjustment from you to get it right. Commented Oct 28, 2023 at 16:54
  • Get your js running directly in devtools first. github.com/cefsharp/CefSharp/wiki/… Commented Oct 29, 2023 at 3:12
  • 1
    @amaitland yup thanks for that. Got it working. JS was buggy and after fixing it all seems fine now. Thank you very much. Commented Nov 3, 2023 at 14:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.