Not found exception using cssSelector("button[name='btnK']") in 4th Java Training video

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Not found exception using cssSelector("button[name='btnK']") in 4th Java Training video

gjpAnalyst
When I created the GoogleHomePage object using the video, everything worked as expected with one problem:

when the statement:  String buttonLabel = firefoxDriver.findElement(getSearchButton()).getText();   in the
getSearchButtonLabel method executes an "element not found" exception is thrown.

The searchButton identifier is:  private By searchButton = By.cssSelector("button[name='btnK']");

When I inspect the element for the button on the web page, I get:

<input value="Google Search" aria-label="Google Search" name="btnK" jsaction="sf.chk" type="submit">

which confirms the elements name as btnK.  

Here is the stack trace when the exception occurs:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":"button[name='btnK']"}
Command duration or timeout: 10.10 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.49.1', revision: '7203e46', time: '2016-01-21 17:35:35'
System info: host: 'Greg-PC', ip: '172.16.17.31', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_65'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=44.0.2, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 5dd5b89b-98af-4342-b9f3-7c36292424f4
*** Element info: {Using=css selector, value=button[name='btnK']}
        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:422)
        at org.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.findElementByCssSelector(RemoteWebDriver.java:492)
        at org.openqa.selenium.By$ByCssSelector.findElement(By.java:430)
        at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:355)
        at ClassesAndObjects.GoogleHomePage.getSearchButtonLabel(GoogleHomePage.java:80)
        at ClassesAndObjects.TestClass.main(TestClass.java:47)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":"button[name='btnK']"}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.49.1', revision: '7203e46', time: '2016-01-21 17:35:35'
System info: host: 'Greg-PC', ip: '172.16.17.31', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_65'
Driver info: driver.version: unknown
        at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/Greg/AppData/Local/Temp/anonymous1462797320737497250webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10681)
        at <anonymous class>.fxdriver.Timer.prototype.setTimeout/<.notify(file:///C:/Users/Greg/AppData/Local/Temp/anonymous1462797320737497250webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:623)
Disconnected from the target VM, address: '127.0.0.1:1339', transport: 'socket'

If I change the name to 'btnG', I don't get the exception, but I also don't get a buttonLabel null after execution.

I've tried changing the name to 'btnI', which is the name of the other button on the page, and it still throws the exception.

I've carefully compared my code to yours and have tried stepping through the code in debug, but so far I can't seem to find the problem.  I'm not sure how to proceed from here, so any suggestions would be greatly appreciated.

Also, is there some way to add attachments to these questions?

 
Reply | Threaded
Open this post in threaded view
|

Re: Not found exception using cssSelector("button[name='btnK']") in 4th Java Training video

softwaretestingforum
Administrator
Do you always have this exception?
Did you try to wait for presence of element?
~ seleniumtests.com
Reply | Threaded
Open this post in threaded view
|

Re: Not found exception using cssSelector("button[name='btnK']") in 4th Java Training video

VjVicky
<input value="Google Search" aria-label="Google Search" name="btnK" jsaction="sf.chk" type="submit">

xpath  //input[@name='btnK']
cssSelector input[name='btnK']