I found this interesting resource on how to copy values from one field to another by clicking a button. I attempted to automate the process using Selenium WebDriver. Here is my script:
driver.get("http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onclick_copy");
driver.findElement(By.xpath(".//button[@onclick='myFunction()']")).click();
However, I encountered an 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 an alternative approach:
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.getElementById(\"field2\").value = document.getElementById(\"field1\").value;");
This time, I faced another error:
org.openqa.selenium.WebDriverException: document.getElementById(...) is null