0

When working with OpenQA.Selenium, I need to FindElement() by a CssSelector. That's easy in the case of the folded or multiple classes.

<div class="myClass andMore">
    <div class="hey">
        <div class "deeper">
            inner text 1
        </div>
    </div>
    <div class="hey bad">
        <div class "deeper">
            inner text 2
        </div>
    </div>
    <div class="hey">
        <div class "deeper">
            inner text 3
        </div>
    </div>
</div>

To get "inner text 2", I can do the following:

string innerTxt2 = browserClient.FindElement(By.CssSelector("myClass.andMore .hey.bad .deeper"))
                                .Text;

however, I can't understand how to select all "hey" except the "hey bad", and then select texts inside of their "deeper" divs.

I want to get:

List<string> text1_and_text3 = browserClient.FindElement(By.CssSelector("myClass.andMore.hey:not('hey.bad').deeper)
                                            .Select(el=>el.Text)
                                            .ToList()"

However, an exception is thrown, saying "not a valid selector" Tried to follow this post and this one but no luck

I need to do this purely with css selector query (would like minimum Linq / for-loops)

Kari
  • 1,244
  • 1
  • 13
  • 27

1 Answers1

1

You can get all you need by using xpath:

browserClient.FindElement(By.XPath("//div[contains(@class, 'hey') and not(contains(@class, 'bad'))]/div")