Home > front end >  Selenium C#: How to identify element without particular child element
Selenium C#: How to identify element without particular child element

Time:07-10

I have following elements on the page:

<span >
    <span>No tickets</span>
    <h5 >..</h5>
    <p >..</p>
    <p >..</p>
</span>

There are several the same card-body elements in the DOM, however, I would like to identify the first one that does not contain <span>No tickets</span> child element.

What is the most correct locator which I should use, while working with selenium and C#?

CodePudding user response:

To identify the first element that does not have a decendent <span>No tickets</span> element you can use the following xpath based locator strategy:

//span[@class='card-body'][not(.//span[text()='No tickets'])]

Your effective line of code will be:

driver.FindElement(By.XPath("//span[@class='card-body'][not(.//span[text()='No tickets'])]"))

Snapshot of the example:

ticket

CodePudding user response:

//span[@class='card-body'][not(span[text()='No tickets'])]

Gets the span with class card-body that does not have span with text "No tickets".

  • Related