2

I am trying to find a link that contains: ng-click="showModal()" and has the innerText: 1122334455

I have tried to use the below code but it doesn't find the link(atag). I am not sure what could be the problem in the codeline that uses: document.querySelector? (That line is the problem)

HTML code for where the atag is located and look like:

<td class="col-reservation-id text-center">
  <span class="overflow-text">
    <a class="highlight" ng-click="showModal()">1122334455</a>
  </span>
</td>

C#/javascript code to find the above atag

public async Task<bool> clicklink(ChromiumWebBrowser browser)
{
    String atag = "document.querySelector('" + "a[ng-click=" + '"' + "showModal()" + '"' + "].innerText === '1122334455'" + "')";

    String clickinputscript = atag + ".focus(); " +
                              atag + ".click(); ";

    bool success = false;
    try
    {
        success = (await browser.GetMainFrame().EvaluateScriptAsync(clickinputscript)).Success;
    }
    catch (Exception ex) { success = false; }
    return success;
}
FZs
  • 16,581
  • 13
  • 41
  • 50
Andreas
  • 1,121
  • 4
  • 17
  • 34

2 Answers2

2

You can't place JS code inside document.querySelector's query string.

You have to do it after selection. To do it, use document.querySelectorAll and a for loop instead:

for(const element of document.querySelectorAll('a[ng-click="showModal()"]').values()){
  if(element.innerText === '1122334455'){
    element.focus()
    element.click()
    break
  }
};

Or, converted to C#-land:

String clickinputscript = "for(const element of document.querySelectorAll('a[ng-click=" + '"showModal()"'+"]').values()){if(element.innerText === '1122334455'){element.focus();element.click();break;};};";
FZs
  • 16,581
  • 13
  • 41
  • 50
  • 1
    That is beautiful. I understand now what you mean. This did work out perfectly!. Thank you very much for your help FZs! – Andreas Sep 28 '19 at 16:41
-1

It's probablly because ng-click is not propper html attribute, try to find it by id or class :)

Darek Gala
  • 157
  • 1
  • 2
  • 7
  • 1
    I know it is possible to use ng-click attribute to find the tag but some other thing is wrong in my code :) – Andreas Sep 28 '19 at 16:21