Ak17
Ak17

Reputation: 85

Webelement identification in Xpath

I am working on xpath and I have a query regarding this. Below is the relevant html code for the getting input to a text box:

<div id="formId:fld_SHIPPING_ADDRESS__PHONE_PRIMARY_id_wrapper" 
     class="fld_SHIPPING_ADDRESS__PHONE_PRIMARY_id ">
  <label id="formId:lbl_SHIPPING_ADDRESS__PHONE_PRIMARY_id" style="" 
         for="formId:fld_SHIPPING_ADDRESS__PHONE_PRIMARY_id">
  **<input id="formId:fld_SHIPPING_ADDRESS__PHONE_PRIMARY_id" type="text" maxlength="20" 
           value="" name="formId:fld_SHIPPING_ADDRESS__PHONE_PRIMARY_id"/>**
  <span id="formId:fld_SHIPPING_ADDRESS__PHONE_PRIMARY_id_msg"/>
</div>

I wrote the Xpath as

 driver.findElement(
     By.xpath("//input[@id = 'formId:fld_SHIPPING_ADDRESS__PHONE_PRIMARY_id')]"))
.sendKeys("test");

This is the only matching node in the html and when I run it I'm getting the below error

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//input[@id = 'formId:fld_SHIPPING_ADDRESS__PHONE_PRIMARY_id']"} Command duration or timeout: 104.80 seconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html

Kindly help for the same!

Upvotes: 0

Views: 219

Answers (2)

StrikerVillain
StrikerVillain

Reputation: 3776

I just checked your source code and found that your input box is contained within a frame.

Use the following code :-

//switch to the frame
driver.switchTo().frame("envoy");   

//get the phone number field and enter data 
driver.findElement(By.id("formId:fld_SHIPPING_ADDRESS__PHONE_PRIMARY_id")).sendKeys("");

In the above code I have used id as locator. I'd recommend that you use name or id over xpath. Your web application under test is beautifully coded with proper id and name tags!!

Let me know if this is useful for you.

Upvotes: 1

barak manos
barak manos

Reputation: 30136

You can start by fixing the following issues in your code:

  1. Close the <label> tag with a corresponding </label> tag.

  2. Remove the two spaces in the @id = string.

If the above solves the NoSuchElementException that you're getting, then you will probably also need to extend .sendKeys("test") into .sendKeys("test").submit(), in order to complete this issue.

Upvotes: 0

Related Questions