For understanding working with PhantomJS better I tried it for an simple Google search today. These are the steps:
- open Google (working)
- make a screenshot (for validating the page has been load) (working)
- Send keys "Hello" (not working)
This is my code:
System.setProperty("phantomjs.binary.path", "webdriver/phantomjs.exe");
WebDriver driver = new PhantomJSDriver();
driver.get("http://google.com");
Thread.sleep(20000);
File file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileCopyUtils.copy(file, new File("C:/Users/username/Desktop/screenshot.jpg"));
Actions a = new Actions(driver);
a.moveToElement(driver.findElement(By.xpath("//*[@id=\"lst-ib\"]"))).click().perform();
a.sendKeys("Hello").perform();
I'm trying to click the textfield and type in "Hello". But I only get the error (sorry, I got a "NoSuchElementException", not a "NullPointerException")
org.openqa.selenium.NoSuchElementException: {"errorMessage":"Unable to find element with xpath
'//[@id=\"lst-ib\"]'","request":{"headers":{"Accept-Encoding":"gzip,deflate","Connection":"Keep-Alive","Content-Length":"47","Content-Type":"application/json;
charset=utf-8","Host":"localhost:39266","User-Agent":"Apache-HttpClient/4.5.1 (Java/1.8.0_66)"},"httpVersion":"1.1","method":"POST","post":"{\"using\":\"xpath\",\"value\":\"//[@id=\\"lst-ib\\"]\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/1fae7fb0-3651-11e7-a707-9fe5bd25a173/element"}}
Command duration or timeout: 303 milliseconds For documentation on
this error, please visit:
http://seleniumhq.org/exceptions/no_such_element.htmorg.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at
org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at
org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
at
org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
at
org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:500)
at org.openqa.selenium.By$ByXPath.findElement(By.java:361) at
org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355)
at
testExamples.SeleniumPhantomJSHeadlessTest.init(SeleniumPhantomJSHeadlessTest.java:233)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497) at
org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at
org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:510)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:211)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:138)
at
org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:170)
at
org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:104)
at org.testng.TestRunner.privateRun(TestRunner.java:774) at
org.testng.TestRunner.run(TestRunner.java:624) at
org.testng.SuiteRunner.runTest(SuiteRunner.java:359) at
org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354) at
org.testng.SuiteRunner.privateRun(SuiteRunner.java:312) at
org.testng.SuiteRunner.run(SuiteRunner.java:261) at
org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at
org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at
org.testng.TestNG.runSuitesSequentially(TestNG.java:1215) at
org.testng.TestNG.runSuitesLocally(TestNG.java:1140) at
org.testng.TestNG.run(TestNG.java:1048) at
org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)
Caused by: org.openqa.selenium.remote.ScreenshotException: Screen shot has
been taken Build info: version: '2.53.0', revision: '35ae25b', time:
'2016-03-15 16:57:40' System info: host: '', ip:
'', os.name: 'Windows 7', os.arch: 'x86', os.version:
'6.1', java.version: '1.8.0_66' Driver info: driver.version:
RemoteWebDriver at
org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:138)
... 30 more
Caused by: org.openqa.selenium.NoSuchElementException:
{"errorMessage":"Unable to find element with xpath
'//[@id=\"lst-ib\"]'","request":{"headers":{"Accept-Encoding":"gzip,deflate","Connection":"Keep-Alive","Content-Length":"47","Content-Type":"application/json;
charset=utf-8","Host":"localhost:39266","User-Agent":"Apache-HttpClient/4.5.1 (Java/1.8.0_66)"},"httpVersion":"1.1","method":"POST","post":"{\"using\":\"xpath\",\"value\":\"//[@id=\\"lst-ib\\"]\"}","url":"/element","urlParsed":{"anchor":"","query":"","file":"element","directory":"/","path":"/element","relative":"/element","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/element","queryKey":{},"chunks":["element"]},"urlOriginal":"/session/1fae7fb0-3651-11e7-a707-9fe5bd25a173/element"}}
For documentation on this error, please visit:
http://seleniumhq.org/exceptions/no_such_element.html
Update 12.05.2017: I found a solution there: http://it-kosmopolit.de/blog/2014/02/25/quick-start-of-running-selenium-webdriver-against-phantomjs-in-java-on-windows-plain-java/
Looking after WebElement using findElementBy.name("q") worked for me.