MLShivaPrasad MLShivaPrasad - 1 year ago 159
Java Question

Unable to execute Selenium WebDriver command on Javascript function enabled button

I am referring to On clicking the button, it is supposed to copy the value in field1 to field2. I tried to automate it with selenium webdriver.
I am using

commands as:


But I got the error:

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":".//button[@onclick='myFunction()']"}
Command duration or timeout: 20.11 seconds

After that I tried:

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.getElementById(\"field2\").value = document.getElementById(\"field1\").value;");

This time I got the error:

org.openqa.selenium.WebDriverException: document.getElementById(...) is null

Answer Source

Your button is inside an iframe. You have to switch to the frame before find the element. See code below:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download