java - Selenium Xpath to find a table cells inside a div tag -


i have following html code need check if text exists within table cell:

<div class="background-ljus" id="autotext">   <table class="topalignedcellcontent">     <tbody>       <tr>         <td>x1</td>       </tr>       <tr>         <td>x2</td>       </tr>      </tbody>   </table>   <table>     <tbody>       <tr>         <td>y1</td>         <td>y2</td>       </tr>     </tbody>   </table>   <table>     <tbody>       <tr>         <td>z1</td>         <td>z2</td>       </tr>     </tbody>   </table> </div> 

i have solved this:

by locator = getlocator(commonconst.xpath, "*//div[@" + type + "='" + anyname + "']");   fluentwait(locator);    webelement div = getdriver().findelement(locator);   list<webelement> cells = div.findelements(by.tagname("td"));   (webelement cell : cells) {     if (cell.gettext().contains(celltext)) {       foundit = true;     }   } 

but think bit slow because need several times. tried xpath had no luck.

"*//div[@id='autotext']//td[contains[text(), 'celltext']]" "*//div[@id='autotext']//table//tbody//tr[td//text()[contains[., 'celltext']]" 

anyone have suggestion why xpath isn't working?

wrong:

*//div[@id='autotext']//td[contains[text(), 'celltext']] *//div[@id='autotext']//table//tbody//tr[td//text()[contains[., 'celltext']] 

correct (with shorter alternatives):

//div[@id='autotext']//td[contains(text(), 'celltext')] //div[@id='autotext']//td[contains(., 'celltext')]  //div[@id='autotext']/table/tbody/tr[td//text()[contains(., 'celltext')] //div[@id='autotext']/table/tbody/tr[td[contains(., 'celltext')]] 
  • contains() function
  • text() needs lowercase
  • predicates can nested
  • don't use // when don't have to

note

. refers "this node" and, when given argument string function such contains(), equivalent of string(.). this, however, not @ same text().

string(.) creates concatenation of text nodes inside current node, no matter how nested. text() node test , default selects direct children.

in other words, if current node <td>foo <b>bar</b> baz</td>, contains(text(), 'bar') false. (so contains(text(), 'baz'), different reason.)

contains(., 'bar') on other hand return true, contains(., 'baz').

only when current node contains nothing single text node, text() , . equivalent. of time, want work . instead of text(). set predicates accordingly.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -