1

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.

    1 Answer 1

    3

    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.

    5
    • Firstly, thank you for the indirect answer, however this I have been doing. It's more cases when the actions don't seem to fire off the correct jscript responses (but you are right I should work to do it with actions). For the second answer, that is helpful, however it now raises another question, how can I find a hidden img (or div holding a hidden img) and set its visbility to visible from invisible. Perhaps I should add more code?CommentedApr 20, 2012 at 6:01
    • 1
      That would just be straight up javascript. You have the element object which is just "arguments[0]", then it's a matter of using javascript to modify the html tag's attributes. There's a few ways to hide an element, so you'd need to know what you need to do to unhide it. The simplest case the javascript would look like this: "arguments[0].setAttribute("style","display:block")" or "arguments[0].setAttribute("style","visibility:visible")". A different example might be if you need to instead modify the class for the element to a class that would display your element.
      – Sam Woods
      CommentedApr 20, 2012 at 20:44
    • Thank you, that makes a lot more sense to me now. I have however now got another problem; the element I want to show is hidden until hovered over! I have a solution to this using Actions (I move the cursor to the container which forces a hover call) so I will use that, but wish I could do it with js instead! Cheers.CommentedApr 22, 2012 at 23:05
    • 1
      Unfortunately, there is no simple way in Selenium 2/Webdriver to fire an event (although there has been in previous Selenium versions). I have not yet had a need to do this personally, or I would have gone and figured it out. There may be someone out there who has already figured it out, and I believe it is a feature people are looking at for future releases as well.
      – Sam Woods
      CommentedApr 23, 2012 at 17:06
    • Thanks for that, I have marked your initial response as the answer because it seems the smartest way around this is to avoid it completely by interacting with the UI correctly (Actions and so forth).CommentedApr 23, 2012 at 23:16

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.