xpath - Selenium - difference between @FindBy and WebElement.findElement() -


i using selenium test user interface.

i trying use @findby-annotation. following piece of code works fine:

@findby(how=how.xpath, xpath ="//input[contains(@id,'idofinputfield')]")     private webelement somewebelement;  private void somemethod(){     webelement = somewebelement.findelement(by.xpath("//a[contains(@class, 'ui-spinner-up')][1]"));     webelement span1 = a.findelement(by.xpath("//a[contains(@class, 'ui-spinner-up')][1]"));     webelement span2 = span1.findelement(by.xpath("//span[contains(@class, 'ui-button-text')][1]"));     webelement b = span2.findelement(by.xpath("//span[contains(@class,'ui-icon ui-icon-triangle-1-n')]"));     b.click(); } 

i use following code, because annotation-based, doesnt work, although think same:

@findby(how=how.xpath, xpath ="//input[contains(@id,'idofinputfield')]"         + "//a[contains(@class, 'ui-spinner-up')][1]"         + "//a[contains(@class, 'ui-spinner-up')][1]"         + "//span[contains(@class, 'ui-button-text')][1]"         + "//span[contains(@class,'ui-icon ui-icon-triangle-1-n')]") private webelement somewebelement;  pivate void somemethod(){     somewebelement.click(); } 

what following exception:

  java.lang.runtimeexception: cannot invoke click on element null. cannot find it.     cause: org.openqa.selenium.nosuchelementexception: unable locate element: {"method":"xpath","selector":"//input[contains(@id,'idofinputfield')]//a[contains(@class, 'ui-spinner-up')][1]//span[contains(@class, 'ui-button-text')][1]//span[contains(@class,'ui-icon ui-icon-triangle-1-n')]"}     command duration or timeout: 14 milliseconds     documentation on error, please visit: http://seleniumhq.org/exceptions/no_such_element.html     build info: version: '2.32.0', revision: '6c40c187d01409a5dc3b7f8251859150c8af0bcb', time: '2013-04-09 10:39:28'     system info: os.name: 'linux', os.arch: 'amd64', os.version: '3.5.0-28-generic', java.version: '1.7.0_21'     session id: 92f605a1-0a63-4ba0-b290-ca724b3c6386     driver info: org.openqa.selenium.firefox.firefoxdriver     capabilities [{platform=linux, databaseenabled=true, cssselectorsenabled=true, javascriptenabled=true, acceptsslcerts=true, handlesalerts=true, browsername=firefox, browserconnectionenabled=true, nativeevents=false, webstorageenabled=true, rotatable=false, locationcontextenabled=true, applicationcacheenabled=true, takesscreenshot=true, version=20.0}]         @ path.to.package.utils.stalereferenceawarefielddecorator$stalereferenceawareelementlocator.in      voke(stalereferenceawarefielddecorator.java:86)             @ com.sun.proxy.$proxy9.click(unknown source)             @      ....             @ sun.reflect.nativemethodaccessorimpl.invoke0(native method)             @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57)             @   sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43)         @ java.lang.reflect.method.invoke(method.java:601)         @ org.junit.runners.model.frameworkmethod$1.runreflectivecall(frameworkmethod.java:44)         @ org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:15)         @ org.junit.runners.model.frameworkmethod.invokeexplosively(frameworkmethod.java:41)         @ org.junit.internal.runners.statements.invokemethod.evaluate(invokemethod.java:20)         @ org.junit.runners.parentrunner.runleaf(parentrunner.java:263)         @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:69)         @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:48)         @ org.junit.runners.parentrunner$3.run(parentrunner.java:231)         @ org.junit.runners.parentrunner$1.schedule(parentrunner.java:60)         @ org.junit.runners.parentrunner.runchildren(parentrunner.java:229)         @ org.junit.runners.parentrunner.access$000(parentrunner.java:50)         @ org.junit.runners.parentrunner$2.evaluate(parentrunner.java:222)         @ org.junit.internal.runners.statements.runbefores.evaluate(runbefores.java:28)         @ org.junit.internal.runners.statements.runafters.evaluate(runafters.java:31)         @ org.junit.runners.parentrunner.run(parentrunner.java:292)         @ org.eclipse.jdt.internal.junit4.runner.junit4testreference.run(junit4testreference.java:50)         @ org.eclipse.jdt.internal.junit.runner.testexecution.run(testexecution.java:38)         @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:467)         @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:683)         @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.run(remotetestrunner.java:390)         @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.main(remotetestrunner.java:197) 

can body explain whats reason behavious or if there difference between 2 pieces of code?

first of all, cannot use such convoluted xpath, kills readability of page objects. selectors should concise possible. secondly, should not use xpath , use css selector. coming question,

 @findby(css="span[class*='ui-icon-triangle-1-n']")  webelement b; 

or try

 @findby(css="span[class*='ui-button-text']>span[class*='ui-icon-triangle-1-n']")  webelement b; 

Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -