I am using Selenium WebDriver and want to execute some javascript from a webpage. I have found a fair few (very useful) posts on executing javascript and have had some success, though I seem to struggle when I have to call javascript from objects on the page (I am new to this so my terminology and basic understand may be off?). The following is the javascript on the page I want to call:
$j(".webGrid tr").hover(function () {
$j(this).find("#imgEdit").css("visibility", "visible");
}
And I currently feel that my two closest attempts are:
js.ExecuteScript("('.itemId').find('#imgEdit').css('visibility', 'visible')"); //1
js.ExecuteScript("(arguments[0]).find('#imgEdit').css('visibility', 'visible')", element); //2
Can anyone give me an idea of where I am going wrong? In the first case I am getting a “.itemId”.find is not a function and in the second arguments[0].find is not a function. I see that ‘find’ is the issue potentially, but it is in the pages javascript file so I’ve got something wrong in my understanding.
An indirect answer but why not just use the ActionBuilder to perform the hover over an element?
Actions builder = new Actions(driver);
builder.moveToElement(someElement);
builder.build().perform();
A more direct answer… find must be a function from a javascript library, and not something available by default through the browser. If you were to do something like this:
return ((IJavaScriptExecutor)webDriverInstance).ExecuteScript("return arguments[0].innerHTML", elementInstance).ToString();
It would work because the innerHTML property is available from any javascript element object.