0

I have class name active and then there is unique text called active text in span(which is nested). Class name active is the unique among other class names then nested text is unique. How would i click on that. I have used following methods.

  FindElement(By.XPath("//li[@class='active']//*[contains(.,'active text')]"));

also i tried

  findelement(BY.xpath(//li[@class='active']//div//div//div//span[contains(.,'active text')]"))

also tried this

  FindElement(By.XPath("//li[contains(@class,'active')] and //span[contains(.,'active text')]")).Text;

Every time i get no such element found ANythoughts this is the html code

<li class="active">
 <div class="a">
  <div class="b">
   <div class="c">
    <h1></h1>
     <h3 class="d"> some text</h3>
      <div class="e">
       <span class="f">
         Active Text</span>
</div></div></div></div>
</li>
Newbie
  • 13
  • 3

3 Answers3

0

You can use either of the following Locator Strategies:

  • CssSelector:

    FindElement(By.CssSelector("li.active span.f"));
    
  • XPath 1:

    FindElement(By.XPath("//li[@class='active']//span[normalize-space()='Active Text']"));
    
  • XPath 2:

    FindElement(By.XPath("//li[@class='active']//span[@class='f' and normalize-space()='Active Text']"));
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
0
FindElement(By.XPath("//li[@class='active']//span[contains(text(),'Active Text')]"));

OR

FindElement(By.XPath("//li[@class='active']//span[@class='f' and contains(text(),'Active Text')]"));

Please try the above code. both will work. also, let me know if clarification is required

Krunal Patel
  • 207
  • 3
  • 10
0

So what worked for me was this,

FindElement(By.CssSelector("li.active")).FindElement(By.XPath("//span[contains(.,'Active Text')]"));

Newbie
  • 13
  • 3