javascript - PhantomJS;使用 Java 单击元素

标签 javascript java selenium nullpointerexception

我正在尝试使用 Java 使用 selenium 来自动化 headless 测试用例。如 PhantomJS; click an element 中所述,使用 PhantomJS 无法简单地单击。没有单击该元素,而是抛出 NullPointerException。

不幸的是,我找到的答案仅解释了 Javascript 中的解决方案。

有人找到 Java 的解决方案吗?

谢谢并致以诚挚的问候

AcMuDD

最佳答案

为了更好地理解 PhantomJS 的使用,我今天尝试了一个简单的 Google 搜索。步骤如下:

  1. 打开 Google(正在运行)
  2. 制作屏幕截图(用于验证页面已加载)(工作)
  3. 发送键“Hello”(不起作用)

这是我的代码:

    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();

我正在尝试单击文本字段并输入“Hello”。但我只收到错误(抱歉,我收到“NoSuchElementException”,而不是“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

更新 12.05.2017:我在那里找到了解决方案:http://it-kosmopolit.de/blog/2014/02/25/quick-start-of-running-selenium-webdriver-against-phantomjs-in-java-on-windows-plain-java/

使用 findElementBy.name("q") 来管理 WebElement 对我来说很有效。

关于javascript - PhantomJS;使用 Java 单击元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43894060/

相关文章:

javascript - 使用 Javascript 正则表达式检查数字模式

javascript - react : Cannot assign to read only property '_status' of object '#<Object>'

javascript - jquery 查找 img 大小并设置为 div css 高度和宽度

java - 无法解释的 Java HashMap 行为

java - 用 1 行代码实例化和初始化对象数组

azure - 如何删除 Azure AD MFA 以进行端到端功能自动化测试

laravel - 经过测试,Chrome进程在Docker容器中仍然保留Laravel Dusk

javascript - 使用 AngularJS 访问 .animation 中的 $scope

java - jnius.JavaException : JVM exception occurred: . ..(系统找不到指定的文件)

python - Selenium xpath 与 Python firefox 驱动程序的链接未单击