-1
<div class="card-header bg-info">
<h4>Dumpytext</h4> 
<h5> Dump text/ mg 
<span style="font-size: 0.9rem;">(100mg/ 50mg)</span> 
</h5>
</div>

I want to hit the h5 and span data in selenium c# and xpath without index

músic a
  • 1
  • 2

2 Answers2

0

Use driver.findElement(By.Xpath(//h5)); and driver.findElement(By.Xpath(//h5));

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Both your XPath expressions are trying to locate same node `h5`. Also, the XPath expression should be enclosed within quotes `"//h5"` – Shawn Feb 16 '23 at 16:06
0

Given the HTML:

<div class="card-header bg-info">
    <h4>Dumpytext</h4> 
    <h5> Dump text/ mg 
        <span style="font-size: 0.9rem;">(100mg/ 50mg)</span> 
    </h5>
</div>

To print the text from <h4>, <h5> and <span> tags you can use either of the following locator strategies:

  • XPath for <h4> tag:

    Console.WriteLine(driver.FindElement(By.XPath("//h4")).Text);
    
  • XPath for <h5> tag:

    Console.WriteLine(driver.FindElement(By.XPath("//h5")).Text);
    
  • XPath for <span> tag:

    Console.WriteLine(driver.FindElement(By.XPath("//h5/span")).Text);
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352