如何将CSS选择器与"not"一起使用在C#中? [英] How to use css selector with "not" in c#?

查看:116
本文介绍了如何将CSS选择器与"not"一起使用在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过CSS选择器查找元素,但是我想跳过其中的一些元素,我该怎么做?

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]")) 

但是我不需要带已注销和客户/合同的href

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

推荐答案

您可能不需要LINQ.您可以使用:not()伪类,为每个要排除的值使用一个伪类(请注意每个否定项中的*=;我假设此处与子字符串匹配):

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'])"))

一个例外是,如果您有一个要排除的值的动态列表:您可以使用string.Join()或循环以编程方式构建选择器,然后将其传递给By.CssSelector(),或者可以遵循Richard Schneider的回答使用LINQ保持事物清洁.

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选择器与"not"一起使用在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆