I have a problem with the following situation. I want to execute a JS Click on my C# Selenium Element but my click is not being properly invoked, I dont understand the call convention on the ExecuteScript function to properly send the needed arguments.
This is the JS Function I am trying to invoke
function clickTab(obj, conteudo, e)
{
var sel;
if(e.target)//Firefox
{
sel = e.target.id;
}
else//IE
{
sel = e.srcElement.id;
}
var count = obj.childNodes.length;
for(i = 0; i < count; i++)
{
var node = document.getElementById(obj.childNodes[i].id);
if(node){
var numNode = node.id.split('-');
if(node.id == sel)
{
node.className = 'selected';
var countC = conteudo.childNodes.length;
for(j = 0; j < countC; j++)
{
var nodeC = document.getElementById(conteudo.childNodes[j].id);
if(nodeC){
var numNodeC = nodeC.id.split('-');
if(numNode[numNode.length-1] == numNodeC[numNodeC.length-1])
{
nodeC.className = 'selected';
}
else
{
nodeC.className = '';
}
}
}
}
else
{
node.className = '';
}
}
}
}
This is the HTML wrapper call to the JS Function
<ul id="ctl00_ctl00_tabs" class="tabs" onclick="javascript:clickTab(this, document.getElementById('contents'), event)">
<li class="selected" id="tab-1" style="position: relative; z-index: 1">Var1</li>
<li id="tab-2" style="position: relative; z-index: 1">Var2</li>
<li id="tab-3" style="position: relative; z-index: 1">Var3</li>
</ul>
This is my C# attempt to Execute the click to select the proper tab but the driver.ExecuteScript triggers an exception on driver.ExecuteScript.
IWebElement buttonToClick = driver.FindElement( By.XPath("//ul[ @id='ctl00_ctl00_tabs' and @class='tabs' ]" );
string script = buttonToClick.GetAttribute( "onclick" );
driver.ExecuteScript( script, "tab-2" );
Any help or hint will be greatly appreciated. Thank you.