Javascript 幻影;使用Java单击一个元素
我正在尝试使用Java,使用selenium自动化测试用例。如中所述,对于PhantomJS,简单地单击是不可能的。不会单击元素,而是引发NullPointerException 不幸的是,我找到的答案只能解释Javascript中的解决方案 有人找到了Java的解决方案吗 谢谢并致以最良好的问候Javascript 幻影;使用Java单击一个元素,javascript,java,selenium,nullpointerexception,Javascript,Java,Selenium,Nullpointerexception,我正在尝试使用Java,使用selenium自动化测试用例。如中所述,对于PhantomJS,简单地单击是不可能的。不会单击元素,而是引发NullPointerException 不幸的是,我找到的答案只能解释Javascript中的解决方案 有人找到了Java的解决方案吗 谢谢并致以最良好的问候 AcMuDD为了更好地理解如何使用PhantomJS,我今天尝试了一个简单的谷歌搜索。以下是步骤: 打开谷歌(工作) 制作屏幕截图(用于验证页面是否已加载)(正在运行) 发送键“你好”(不工作) 这是
AcMuDD为了更好地理解如何使用PhantomJS,我今天尝试了一个简单的谷歌搜索。以下是步骤:
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”。但我只得到了错误(对不起,我得到了一个“NoTouchElementException”,而不是“NullPointerException”)
org.openqa.selenium.NoSuchElementException:{“errorMessage”:“找不到使用xpath的元素
“//[@id=\'lst ib\'”,“request”:{“headers”:{“Accept Encoding”:“gzip,deflate”,“Connection”:“Keep Alive”,“Content Length”:“47”,“Content Type”:“application/json;
charset=utf-8,“主机”:“本地主机:39266”,“用户代理”:“Apache HttpClient/4.5.1(Java/1.8.0_66)”,“httpVersion”:“1.1”,“方法”:“POST”,“POST”:“{\”使用\“:\”xpath \“,\“值\“:\”/[@id=\“lst ib\\”]\”,“url”:“/element”,“urlParsed”:“{”锚定“,“查询”:“,“文件”:“元素”,“目录”:“/”路径“/”元素”,“相对“:”端口”:“主机”:,“密码”:“用户”:“用户信息”:“权限”:“协议”:“源”:“/element”、“queryKey”:{},“块”:[“element”]},“urlOriginal”:“/session/1fae7fb0-3651-11e7-a707-9fe5bd25a173/element}”
命令持续时间或超时:303毫秒用于
此错误,请访问:
在
org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
在
org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
在
org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
在
org.openqa.selenium.remote.RemoteWebDriver.findelementbypath(RemoteWebDriver.java:500)
位于org.openqa.selenium.By$ByXPath.findElement(By.java:361)
org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355)
在
testExamples.seleniummbantomjsheadlesstest.init(seleniummbantomjsheadlesstest.java:233)
位于的sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)
invoke(NativeMethodAccessorImpl.java:62)
在
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
位于java.lang.reflect.Method.invoke(Method.java:497)
org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
在
org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:510)
位于org.testng.internal.Invoker.invokeConfigulations(Invoker.java:211)
位于org.testng.internal.Invoker.invokeConfigulations(Invoker.java:138)
在
org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:170)
在
org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:104)
位于org.testng.TestRunner.privateRun(TestRunner.java:774)
org.testng.TestRunner.run(TestRunner.java:624)位于
org.testng.SuiteRunner.runTest(SuiteRunner.java:359)位于
org.testng.SuiteRunner.runsequential(SuiteRunner.java:354)位于
org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)位于
org.testng.SuiteRunner.run(SuiteRunner.java:261)位于
org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)位于
org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)位于
org.testng.testng.runSuitesSequentially(testng.java:1215)位于
org.testng.testng.runSuitesLocal(testng.java:1140)位于
org.testng.testng.run(testng.java:1048)位于
org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
位于org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
位于org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)
原因:org.openqa.selenium.remote.ScreenshotException:屏幕截图已被删除
已获取生成信息:版本:'2.53.0',修订版:'35ae25b',时间:
“2016-03-15 16:57:40”系统信息:主机:“”,ip:
'',os.name:'Windows 7',os.arch:'x86',os.version:
“6.1”,java.version:“1.8.0_66”驱动程序信息:Driver.version:
RemoteWebDriver位于
org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:138)
…30多个
原因:org.openqa.selenium.NoSuchElementException:
{“errorMessage”:“找不到具有xpath的元素
“//[@id=\'lst ib\'”,“request”:{“headers”:{“Accept Encoding”:“gzip,deflate”,“Connection”:“Keep Alive”,“Content Length”:“47”,“Content Type”:“application/json;
charset=utf-8,“主机”:“本地主机:39266”,“用户代理”:“Apache HttpClient/4.5.1(Java/1.8.0_66)”,“httpVersion”:“1.1”,“方法”:“POST”,“POST”:“{\”使用\“:\”xpath \“,\“值\“:\”/[@id=\\\\”lst ib\\\”]\“}”,“url”:“/”元素”,“url解析“:{”锚定“,“查询“:”文件“:”元素“,“目录“/”路径“/”元素“,”相对“:”元素“:”端口“:”主机“:”密码“:”用户“:”用户信息“:”权限“:”协议“:”源“/”元素“,”查询键“{}”,块“[”元素“]}”,URLEORIGINAL“/”会话/1fae7fb0-3651-11e7-a707-9fe5bd25a173/元素“}”
有关此错误的文档,请访问:
2017年5月12日更新:我在那里找到了一个解决方案:
使用findElementBy.name(“q”)查看WebElement对我来说很有用。为了更好地理解使用PhantomJS,我今天尝试了一个简单的谷歌搜索。以下是步骤:
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”。但我只得到了错误(对不起,我得到的是“NoTouchElementException”,而不是“NullPointerException”)