Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery click() on anchor tag not firing from an injected script inside WebBrowser control

I am developing a web bot using WinForms WebBrowser control. Everything is working fine except for the second click() call in following code:

function SaveRecordClick() {
    try {
        var menuButton = $('#s_at_m_4');
        menuButton.click();             //<-- This is working
        var x = $('#s_at_m_4-menu li')[5];
        var saveLink = $(x).find('a')[0];
        if (saveLink != null){
            saveLink.click();           //<-- This is not working

            return "1";
        }
    }
    catch (err) {
        alert(err.message);
    }
    return "0";
}

saveLink is not null. I know this because I placed an alert() call inside the condition.

Updated code with suggested solutions

function SaveRecordClick() {
    try {
        var menuButton = $('#s_at_m_4');
        menuButton.click();
        var x = $('#s_at_m_4-menu li').eq(5);
        var saveLink = x.find('a').eq(0);
        if (saveLink != null) {
            alert(saveLink.html());
            saveLink.click();

            return "1";
        }
    }
    catch (err) {
        alert(err.message);
    }
    return "0";
}

But still the same problem. 'alert()' is working fine but html() is showing inner text instead of HTML of the anchor.

HTML Code

<li data-caption="Save Record                [Ctrl+S]" class="sbui-appletmenu-item  ui-menu-item" role="presentation">
<a href="javascript:void(0)" aria-disabled="false" class="ui-corner-all" id="ui-id-254" tabindex="-1" role="menuitem">Save Record                [Ctrl+S]</a>
</li>

ID of the anchor tag is dynamically generated.

Also, click() is triggering when the same code is executed from Google Chrome Console. So, could it be the issue with the WebBrowser control?

Update

I think guys its a problem with the webrowser control that inherits Internet Explorer. So now I am shifting to another browser control and testing the code on it. Will let you know if it works there.

like image 439
Aishwarya Shiva Avatar asked May 26 '17 15:05

Aishwarya Shiva


2 Answers

You have to wrap your object with jQuery in order to make it work:

var saveLink = $($(x).find('a')[0]);
like image 75
Ursache Avatar answered Oct 06 '22 00:10

Ursache


Try with Below condition's:

  1. trigger('click') instead of click
  2. Change the if condition validation with if(saveLink) .Its validate both null,undefined all false statement
  3. Return with number=> 0 instead of sting =>'0'
  4. anchor tag redirection use with window.location.href and get the href value from anchor try with attr('href')
function SaveRecordClick() {
  try {
    var menuButton = $('#s_at_m_4');
    menuButton.trigger('click');
    var x = $('#s_at_m_4-menu li').eq(5);
    var saveLink = x.find('a').eq(0);
    if (saveLink) {
      alert(saveLink.html());
      window.location.href=saveLink.attr('href');
     return 1;
    }
  } catch (err) {
    alert(err.message);
  }
  return 0;
}

working example

function SaveRecordClick() {
  try {
    var menuButton = $('#s_at_m_4');
    menuButton.trigger('click');
    var x = $('#s_at_m_4-menu li').eq(5);
    var saveLink = x.find('a').eq(0);
    if (saveLink) {
      alert(saveLink.html());
      window.location.href=saveLink.attr('href');
     return 1;
    }
  } catch (err) {
    alert(err.message);
  }
  return 0;
}
SaveRecordClick()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="s_at_m_4-menu">
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li data-caption="Save Record                [Ctrl+S]" class="sbui-appletmenu-item  ui-menu-item" role="presentation">
    <a href="https://example.com" aria-disabled="false" onclick="console.log('s')" class="ui-corner-all" id="ui-id-254" tabindex="-1" role="menuitem">Save Record                [Ctrl+S]</a>
  </li>
  <li></li>
</ul>
like image 27
prasanth Avatar answered Oct 06 '22 00:10

prasanth