2

I am using execelementasync in c#.

I want to use JavaScript too simulate a click to this button:

<a class="btn confirm" href="#">
  <h5>
    Begin
  </h5>
</a>

My code:

string jsScriptB = System.Xml.Linq.XElement.Parse(@"<js><![CDATA[ document.getElementByClassName('btn confirm').click();]]></js>").Value;
            browser.ExecuteScriptAsync(jsScriptB);

I am really not sure what is going wrong but the button does not click.

Question: How can I click that button using JavaScript

jwpfox
  • 4,786
  • 11
  • 41
  • 42
Billy Bob
  • 35
  • 8

1 Answers1

1

In Javascript document.getElementsByClassName will return a array of HtmlElement so you can't call click on it directly. you need to click on a single element

document.getElementsByClassName('btn confirm')[0].click();

Secondly you are trying to use 2 class in your getElementByClassName which is invalid in first place.

You can better call it like

document.querySelectorAll('.btn,.confirm')

or just call querySelector if there is only one element in your html document. later you can call it like

browser.Document.GetElementById(".btn,.confirm").InvokeMember("click"); 
Anirudha Gupta
  • 8,248
  • 8
  • 49
  • 71
  • thank you, that solves it. I had tried adding [0] based on a similar question but foolishly made a typo and said document.getElementByClassName('btn confirm')[0].click(); (element, not elements). Anyway, thank you! – Billy Bob Jan 03 '18 at 02:31