首页 新闻 搜索 专区 学院

selenium-java 元素一直提示定位不到

0
悬赏园豆:50 [已解决问题] 解决于 2017-12-20 14:04

seleniuum IDE上抓取的代码,在Java上运行以后一直提示无法定位元素,这个应该怎么解决?(刚学自动化测试,有很多不懂得,望指教)

报错信息:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"name_l2"}
(Session info: chrome=61.0.3163.79)
(Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 30.03 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'LXB-PC', ip: '192.168.1.8', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_112'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f), userDataDir=C:\Users\lxb\AppData\Local\Temp\scoped_dir5428_18117}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=61.0.3163.79, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, setWindowRect=true, unexpectedAlertBehaviour=}]
Session ID: c9f24d44ca92edcffd2772518a126007
*** Element info: {Using=id, value=name_l2}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:215)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:167)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:671)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:410)
at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:453)
at org.openqa.selenium.By$ById.findElement(By.java:218)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:402)
at testecc.login.test1(login.java:38)
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:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

 

代码和页面源码如下:

 

夏末秋至的主页 夏末秋至 | 初学一级 | 园豆:106
提问于:2017-12-10 13:02
< >
分享
最佳答案
0

找不到name_l2这个id,建议通过classname定位试试,再不行用xpath

收获园豆:50
ycyzharry | 高人七级 |园豆:22230 | 2017-12-11 09:34

都试过了,还是不行;看的是跳转到新的页面以后就定位不到元素了

夏末秋至 | 园豆:106 (初学一级) | 2017-12-11 11:27

@夏末秋至: 跳转到新页面 要做句柄判断的:

String currentWindow = driver.getWindowHandle();
            Set<String> handles = driver.getWindowHandles();
            Iterator<String> it = handles.iterator();
            while (it.hasNext()) {
                String handle = it.next();
                if (currentWindow.equals(handle))
                    continue;
                driver.switchTo().window(handle);
            }
ycyzharry | 园豆:22230 (高人七级) | 2017-12-11 12:04

@ycyzharry: 谢谢,已解决

夏末秋至 | 园豆:106 (初学一级) | 2017-12-20 14:04

@ycyzharry: 你好,抱歉,还有一个问题想要问一下,上次说的是跳转到一个新的页面需要做句柄判断,如果是在同一个页面中打开另一个页面(具体如下图),还是提示定位不到元素,这个应该怎么解决?

夏末秋至 | 园豆:106 (初学一级) | 2018-01-16 11:24

@夏末秋至: 只要是打开新的窗口 就需要做句柄判断  

你可以先试试其他定位方法,比如xpath

ycyzharry | 园豆:22230 (高人七级) | 2018-01-17 08:58
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册