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.
You have to wrap your object with jQuery in order to make it work:
var saveLink = $($(x).find('a')[0]);
Try with Below condition's:
trigger('click')
instead ofclick
- Change the if condition validation with
if(saveLink)
.Its validate bothnull,undefined
all false statement- Return with
number=> 0
instead ofsting =>'0'
anchor
tag redirection use withwindow.location.href
and get thehref
value from anchor try withattr('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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With