Select only span sheet elements through XPath

I use Java and Selenium and testng for Windows 7. My HTML looks like this (it's just part):

<TR>
  <TH>
    <SPAN>Some Text 1</SPAN>
  </TH>
  <TH>
    <SPAN>
      <A id="someid" href="http://someurl">
        <SPAN> Some Text 2</SPAN>
      </A>
    </SPAN>
  </TH>
 </TR>

In other words, "Some Text 1" and "Some Text 2" are at different levels. I am trying to find XPath to find them. I can't use *//TR//SPAN*it because it will get extra SPANover "Some Text 2". I cannot search SPANwith text, because some of them that I find will not have any text. I believe that I can assume that what SPANI'm looking for will always be the lowest level SPANunder TH.

So, can I use XPath? I suppose, instead of using @FindBy, I could do some kind of dynamic recursive loop to find the gap and continue to search for spans under it until I come to the sheet, but that sounds pretty complicated.

+4
source share
3 answers

This XPath

//SPAN[not(*)]

will select all elements SPANthat do not have children, so it will select

<SPAN>Some Text 1</SPAN>
<SPAN>Some Text 2</SPAN>

but not SPANthat contains text 2 SPANas requested.

If you want to restrict the element SPANselected only for those elements of the "SPAN" sheet that are located somewhere below the elements TH:

//TH//SPAN[not(*)]
+2
source

SPAN,

  • TH
  • SPAN

xpath, :

String xpath = "//TH//SPAN[not(.//SPAN)]"
+1

I do not do xpath, but can provide some css help.

$$("th span:last-of-type")

What this will do is to identify (all) the last child th intervals.

+1
source

Source: https://habr.com/ru/post/1614760/


All Articles