How to use Javascript to expand Google PAA section?

66 Views Asked by At

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.

0

There are 0 best solutions below