0

Hi I have the following snipped of HTML:

<tr rowid="0" class="addedrow rowselected datarow rowfocus">
....

<td positionindex="3" class="primary required">
    <input type="text" class="editablecell" size="15">
</td>

<td positionindex="4" class="required">
    <div class="select2-container editablecell" id="s2id_autogen1" style="width: 99%;" title="">
        <a href="javascript:void(0)" onclick="return false;" class="select2-choice select2-default" tabindex="-1"> 
        </a>
    </div>
</td>

....

I have to insert text into the <input type="text" class="editablecell" size="15"> text field. I am having difficulties in locating this particular WebElement using Selenium in Java.

I have tried this

 WebElement box = driver.findElement(By.xpath("//input[(@type='text') and 
(@class='editablecell')]"));

based on the anser here.

Note: This element is in the following structure:

<html> ....
     <body> ...
         <div id=topLeveWrapper> ...
              <div id=header>  ...
              <div id=contentFrame>  ...
                  <div id=leftSite>  ...
                  <div id=rightSite> ...
                      <div id=pageID_123456> ...
                          <div id=gridViewID_98765> ...
                              <div id=tableWrapper> ...
                                  <div id=twHEADER> ...
                                  <div id=twBODY>   ...
                                      <table >
                                          <thead> ...
                                          <tbody>
                                              <tr rowid=0 ...>
                                                  <td positionindex="3" class ="primary required"> <input type="text" class="editablecell" size="15">
                                                  </td>
                                              ....
                                              </tr> 
                                             ......................

Exeption thrown is:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@type='text' and @class='editablecell']"}
  (Session info: chrome=..)

Info:

For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html

Can anyone guid me on this, please?

Thanks

Dilshad Abduwali
  • 1,388
  • 7
  • 26
  • 47
  • I don't see anything wrong with your XPath. Looks like the web element is inside a frame. Did you check that? – Monika Jul 13 '17 at 06:51
  • @dhssa, could you please provide URL and name of the field in question? – Deepak Jul 13 '17 at 10:46
  • Use firebug and firepath to locate your xpath .I am damn sure it will help you to locate it . You can customize your xpath the way you want . For more details follow this link : https://www.guru99.com/xpath-selenium.html – whatsinthename Jul 13 '17 at 12:20

3 Answers3

2

You could simply use this xpath expression driver.findElement(By.xpath("//input[@class='editablecell']"))

Aventador
  • 54
  • 5
1

Try with the following xpath:

 WebElement box = driver.findElement(By.xpath("//input[@type='text' and @class='editablecell']"));
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • So that means, either the element is within an `iframe` or we have to induce `wait` for the element to render on the HTML DOM to be `clickable` – undetected Selenium Jul 13 '17 at 02:52
  • @DebajanB please have look at the edited question where I have specified the structure of the location of the element in the actual HTML code. – Dilshad Abduwali Jul 13 '17 at 03:11
  • How do this xpath differ from the xpath mentioned in the question ? – Sameer Jul 13 '17 at 06:35
  • 1
    @dhssa From your updated HTML DOM I still don't see any reason to get `NoSuchElementException`. Can you please open up the FireBug/FirePath to check if you can identify the element with the `xpath` on the `Top Window`? If not, then you may have to look for the presence of iframe. Thanks – undetected Selenium Jul 13 '17 at 14:36
  • @Sameer The difference between the `xpaths` are pretty much evident here: `//input[(@type='text') and (@class='editablecell')]` and `//input[@type='text' and @class='editablecell']` – undetected Selenium Jul 13 '17 at 14:43
  • @DebanjanB That is what my question is. The only difference that is evident and I can see is use of round braces in the xpath mentioned in the question. So let me rephrase my question "Do the use of round braces, as used above makes any difference in xpaths ?". I am asking this because when I used round braces in my xpaths, I didnt see much of a change in behaviour of xpaths. – Sameer Jul 14 '17 at 02:31
0

I am answering my own question as I have tried the followinig way to locate the element and it worked well. This may not be the best way to locate the element, however, due to my limited XPATH and Selenium knowledge, I only was able to do it this way. As the HTML structure in the question shows, I started from quite higher level of the elements and drilled down to the desired element I was after:

driver.findElement(By
            .xpath("//div[@class='fht-tbody']/table/tbody/tr[@rowid='0']/td[@positionindex='3']/input")); 

I appreciate comments which corrected my question in many wasy and all comments and answers have helped me and led me to my way of solution and I hope to see better ways to solve this problem.

Thanks everyone!

Dilshad Abduwali
  • 1,388
  • 7
  • 26
  • 47