0

Trying to automate a login page. The HTML for the button is below:

button class="btn loginBtn " style="" tabindex="3" type="submit">Login</button

Java code:

WebDriver wd = new FirefoxDriver();
wd.findElement(By.id("btn loginBtn")).click();

But this throws the below error.

Error:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"btn loginBtn"}
Command duration or timeout: 14 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.42.2', revision: '6a6995d', time: '2014-06-03 17:42:30'
System info: host: 'techteam-PC', ip: '192.168.10.150', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.6.0_23'
Session ID: 8b0100ab-bb50-469c-ada6-ad9f9c21d525
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=firefox, rotatable=false, locationContextEnabled=true, version=30.0, cssSelectorsEnabled=true, databaseEnabled=true, handlesAlerts=true, browserConnectionEnabled=true, nativeEvents=false, webStorageEnabled=true, applicationCacheEnabled=true, takesScreenshot=true}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:352)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:393)
    at org.openqa.selenium.By$ById.findElement(By.java:214)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:344)
    at AutoStaging.main(AutoStaging.java:24)
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Unable to locate element: {"method":"id","selector":"btn loginBtn"}
Build info: version: '2.42.2', revision: '6a6995d', time: '2014-06-03 17:42:30'
System info: host: 'techteam-PC', ip: '192.168.10.150', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.6.0_23'
Driver info: driver.version: unknown
    at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/techteam/AppData/Local/Temp/anonymous8022566380795403314webdriver-profile/extensions/[email protected]/components/driver_component.js:9470:7)
    at <anonymous class>.FirefoxDriver.prototype.findElement(file:///C:/Users/techteam/AppData/Local/Temp/anonymous8022566380795403314webdriver-profile/extensions/[email protected]/components/driver_component.js:9479:3)
    at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/techteam/AppData/Local/Temp/anonymous8022566380795403314webdriver-profile/extensions/[email protected]/components/command_processor.js:11455:11)
    at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/Users/techteam/AppData/Local/Temp/anonymous8022566380795403314webdriver-profile/extensions/[email protected]/components/command_processor.js:11460:7)
    at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/Users/techteam/AppData/Local/Temp/anonymous8022566380795403314webdriver-profile/extensions/[email protected]/components/command_processor.js:11402:5)

2 Answers 2

3

Your button have class loginBtn not id loginBtn Try wd.findElement(By.xpath("//button[contains(@class, 'loginBtn')]")).click();

Sign up to request clarification or add additional context in comments.

Comments

0

Instead of searching id for all the web elements use the firebug add on tool in firefix and get the xPath for it. Then you can use the below code:

wd.findElement(By.xPath("yourXpathUsingFirebug")).click();

2 Comments

id('loginPalette')/x:form/x:fieldset/x:button --- is the xpath for the login button.. wd.findElement(By.xpath("id('loginPalette')/x:form/x:fieldset/x:button")).click(); is my code.. but it still throws an error
no ideally xPath will be in //*[@id='tour-switch-page']/nav/div[2]/h1 format. First go to Mozilla Firefox, Click on Menu Tools->Add-ons.(alternately,Ctrl+Shift+A). Then search for firebug install it and also search firepath and install that as well. Both should get installed and restart firefox. You should be able to see a bug icon at the top right corner of firefox. Navigate to your webpage, click the bug icon. Select Firepath tab in the bottom panel. Select inspect element icon(rectangle box with arrow) and click your button. You see the xPath for that it. Use that in the code I mentioned

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.