1

There is this line of code on a page that I would like to execute:

<a href="javascript:clickOnNode(0,4)" shape=""></a><img name="someimage" src="someimage">

It's a clickable link and I would like to know how to just run that code rather than using "find element by".

My code that I've been running is below, however it doesn't work.

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("clickOnNode(0,4);");

Any ideas on what I'm doing wrong here?

EDITS:

This is the code I'm currently using.

driver.navigate().to("someWebsite.com");

        Thread.sleep(10000);

        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("clickOnNode(0,4);");

        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("img[name='nodeIcon4']")));
        driver.findElement(By.cssSelector("img[name='nodeIcon4']")).click();

Here is the output I am getting when I run the code:

org.openqa.selenium.JavascriptException: Error executing JavaScript

and for the wait for nodeIcon4 lines:

org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.cssSelector: img[name='nodeIcon4'] (tried for 10 second(s) with 500 milliseconds interval)

I also tried running pburgr's code with

        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[contains(@name,'nodeIcon4')]")));
    driver.findElement(By.xpath("//a[contains(@name,'nodeIcon4')]")).click();

and got the following error:

Caused by: org.openqa.selenium.NoSuchElementException: Unable to find element with xpath == //a[contains(@name,'nodeIcon4')]

For more sanity checking:

  • I know that having the hard sleep command is bad. You can scold me later ;-)
  • I am limited to using IE and the webpage automatically emulates IE5. The webpage will not correctly load for anything later than IE5.
  • I am pretty sure that I have security levels for zones correctly setup because I can log on to other sites and interact with elements on the page with my code. Unless the emulation is messing that up somehow.

I can give more details as needed.

11
  • could be just a matter of synchronization. Try solution mentioned here: stackoverflow.com/questions/41425827/… Commented Nov 29, 2018 at 19:24
  • This sounds like an X-Y problem. Instead of asking for help with your solution to the problem, edit your question and ask about the actual problem. What are you trying to do? Commented Nov 29, 2018 at 19:35
  • 1
    Because you failed to execute clickOnNode(0,4) in DevTool's console manually, even you had wait 10 seconds for page load. I think maybe you need to click/expand something on that page which will make the browser to load the javascript file which include the function definition of clickOnNode(). I guess the javascript file includes the clickOnNode() is not loaded when the page opened first time. Commented Nov 30, 2018 at 2:46
  • 2
    What's your problem with finding the element and clicking it? Commented Nov 30, 2018 at 7:21
  • 2
    Can you show your full code that reads the page and the error(s) you're getting? Commented Nov 30, 2018 at 7:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.