1

This is my HTML tag:

<input type="text" maxlength="10" size="10" name="amount">

But I am unable to locate element using Java Selenium web driver. I used all the methods like cssselector, XPath, id, name but no use.

I used driver.findElement(By.cssSelector("input[name=amount]"));

but I'm getting following error:

 *** Element info: {Using=css selector, value=input[name=amount]}
Session ID: 9838c78c121e1eee59c1eb0e48061fbc
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={userDataDir=C:\Users\Welcome\AppData\Local\Temp\scoped_dir4852_17098, chromedriverVersion=2.29.461591 (62ebf098771772160f391d75e589dc567915b233)}, networkConnectionEnabled=false, unexpectedAlertBehaviour=, rotatable=false, locationContextEnabled=true, mobileEmulationEnabled=false, version=58.0.3029.110, pageLoadStrategy=normal, takesHeapSnapshot=true, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=true, browserConnectionEnabled=false, webStorageEnabled=true, nativeEvents=true, hasTouchScreen=false, applicationCacheEnabled=false, 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.findElementByCssSelector(RemoteWebDriver.java:441)
    at org.openqa.selenium.By$ByCssSelector.findElement(By.java:426)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:344)
    at com.totsy.Currencyval.main(Currencyval.java:63)

Can anyone help on this?

2 Answers 2

5

It is important to see what you've tried and what problems did you have, but, judging by what was provided in the question, here is how you can locate the element by "name", by a "CSS selector" and by an "Xpath expression":

driver.findElement(By.name("amount")); 
driver.findElement(By.cssSelector("input[name=amount]"));
driver.findElement(By.xpath("//input[@name='amount']"));

Possible issues:

1

Firstly you have to look that whether element is present is any Frame. If the element is there in any Frame then you have to switch into that particular Frame by using below syntax:

driver().switchTo().frame("frameName");
driver().switchTo().frame("frameIndex");

You can also switch to the frame by giving its element like class, id etc.

driver().switchTo().frame(findElementByClassName("shrGoogleConnectIframe"));

Then you can locate the element using its any of the property mentioned.

driver.findElement(By.name("amount"));
driver.findElement(By.cssSelector("input[name=amount]"));
driver.findElement(By.xpath("//input[@name='amount']"));

Once you are able to locate the element, you have to switch back to the default content using below syntax:

driver.SwitchTo().DefaultContent();

In test automation services, the best practice is to use id, name and CSS selectors, Xpath should be given least priority.

1
  • My first guess with elements that cannot be found is this answer. Double check if it is in a iFrame Jun 22, 2017 at 14:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.