1

I am using selenium to automate some test cases but I am having some issues finding some elements that don't have a certain style. I have created this Xpath that works perfectly in Chrome but when I run it using selenium it doesn't work as I want.

XPATH: ./td[not(contains(@style, 'foo'))]

So basicaly the HTML looks like this:

<tr id='something'>
   <td style='foo'><td/>
   <td style='foo'><td/>
   <td style='foo'><td/>
   <td style='foo'><td/>
   ...
   <td>1<td/>
   <td>2<td/>
   <td>3<td/>
</tr>

So basically I want the last 3 tds so First I get the row and then I sent the Xpath. . . something like this:

var row = driver.FinElement(By.Id("something"));
ReadOnlyCollection<IWebElement> items = row.FindElements(By.Xpath(./td[not(contains(@style, 'foo'))];

But as a result I am getting all the children of the tr.

Any Idea why the Xpath is working on Chrome developer tools and don't work with selenimun IE driver?

Yoiku
  • 882
  • 2
  • 13
  • 25

2 Answers2

1

Compared to Chrome, IE formats the style attribute. For instance, if the style in the HTML is:

<td style='DISPLAY:BLOCK;'>

IE will change it to:

<td style='display: block;'>

So with Chrome //td[@style='DISPLAY:BLOCK;'] will return a match and IE will return no match.

Now, with your example, on solution to get the last 3 elements would be to get all the <td> without a style attribute:

./td[not(@style)]

You could also normalize the casing and remove the spaces with translate:

./td[not(contains(translate(@style,'FO ','fo'), 'foo'))]
Florent B.
  • 41,537
  • 7
  • 86
  • 101
0
  1. Could you try following :

    .//td[not(contains(@style, 'foo')  // double back slash for child element
    

from the docs to access the child

  1. Please make sure that you are not mis matching the case since contains is case-sensitive. Answer to How to use not contains() in xpath?
Community
  • 1
  • 1
Naman
  • 27,789
  • 26
  • 218
  • 353