How to use css selector with “not” in c#?


Tags: c#,selenium-webdriver,css-selectors

Problem :

I want to find elements by css selector, but I want to skip some of them, how can I do it?

driver.FindElements(By.CssSelector("a[href]")) 

but I need not to take href that has logoff and client/contract in it



Solution :

You probably don't need LINQ for this. You can use the :not() pseudo-class, one for each value you want to exclude (note the *= in each negation; I'm assuming substring matches here):

driver.FindElements(By.CssSelector("a[href]:not([href*='logoff']):not([href*='client'])"))

An exception is if you have a dynamic list of values that you want to exclude: you can either build your selector programmatically using string.Join() or a loop and then pass it to By.CssSelector(), or you can follow Richard Schneider's answer using LINQ to keep things clean.


    CSS Howto..

    how to make css for work for different mobile sites

    How does this drop-down have the ^ shown?

    After adding a “current” class to an active item in a menu using jQuery, how can css changes be made to the container of the item?

    How to prevent “jerking” of page in browser

    How to manually override CSS @media queries?

    How to make my .css and .js file work with my html code loaded in a WebView on Android

    How to fix some issues with printing very basic HTML

    How to quickly find a CSS class when there are multiple stylesheets

    How to add vendor prefixed gradients to an element with vanilla JavaScript? [duplicate]

    How to properly center a 100% width div in css

    Rails - how to add css to a helper method

    How to change text from black to white upon hover on background black?

    How can i create a div which contains two divs and stretch automatically

    How can i disable the bootstrap hover color for links

    How to give the color to * without using span Tag

    How to style a outgoing email from contact form with CSS

    How can I get two form fields side-by-side, with each legend above the field?

    How to add an unknown amount of dots to a line depending on length of word?

    how to make the file input field wrap?

    Show/Hide images based on tab selection

    How to load user CSS in a WebKit WebView using PyObjC?

    Extra margin showing below element?

    How to position some text?

    How to rotate pseudo element css

    How to disable Cufon on certain elements?

    How to style HTML select option hover and selected option effects

    How to use the Xpath and CSS selector in for function

    How can I prevent IE from displaying a website? [closed]

    How to reduce the wiDth of dropdown-menu?

    How do I get a zooming effect on an image when a user hovers over it? [closed]