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.
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 ofclickOnNode(). I guess the javascript file includes theclickOnNode()is not loaded when the page opened first time.