当前位置: 首页 > 面试题库 >

不支持复合类名。考虑搜索一个类名并过滤结果

壤驷麒
2023-03-14
问题内容

我正在使用driver.findelement
by.classname方法在firefox浏览器上读取元素,但是我收到“不支持复合类名。请考虑搜索一个类名并过滤结果。” 例外

这是我的代码

driver.FindElement(By.ClassName("bighead crb")).Text.Trim().ToString()

//and here is how the html of browser looks like

<form action="#" id="aspnetForm" onsubmit="return false;">
    <section id="lx-home" style="margin-bottom:50px;">
  <div class="bigbanner">
    <div class="splash mc">
      <div class="bighead crb">LEAD DELIVERY MADE EASY</div>
    </div>
  </div>
 </section>
</form>

问题答案:

不,就您的问题而言,您自己的答案并不是最好的答案。

假设您有这样的HTML:

<div class="bighead ght">LEAD DELIVERY MADE HARD</div>
<div class="bighead crb">LEAD DELIVERY MADE EASY</div>

driver.FindElement(By.ClassName("bighead"))会找到两个,然后返回您的第一个div,而不是您想要的一个。您真正想要的是driver.FindElement(By.ClassName("bighead crb")),但是就像您在问题中说的那样,这将不起作用,因为您需要另一种通过复合类名称查找元素的方法。

这就是为什么大多数人使用功能更强大By.CssSelectorBy.XPath。然后您有:

CssSelector(最好的):

driver.FindElement(By.CssSelector(".bighead.crb")); // flexible, match "bighead small crb", "bighead crb", "crb bighead", etc.
driver.FindElement(By.CssSelector("[class*='bighead crb']")); // order matters, match class contains  "bighead crb"
driver.FindElement(By.CssSelector("[class='bighead crb']")); // match "bighead crb" strictly

XPath(更好):

driver.FindElement(By.XPath(".//*[contains(@class, 'bighead') and contains(@class, 'crb')]")); // flexible, match "bighead small crb", "bighead crb", "crb bighead", etc.
driver.FindElement(By.XPath(".//*[contains(@class, 'bighead crb')]")); // order matters, match class contains string "bighead crb" only
driver.FindElement(By.XPath(".//*[@class='bighead crb']")); // match class with string "bighead crb" strictly


 类似资料:
  • 我正在使用一个驱动程序。通过。classname方法读取firefox浏览器上的元素,但我得到“不支持复合类名。请考虑搜索一个类名并过滤结果。”异常 这是我的代码

  • 我有一个方法来计算< code > div 中元素的数量并返回它们的数目。 这是页面源代码 WebDriver引发以下错误:不支持复合类名。考虑搜索一个类名并过滤结果,或者使用CSS选择器。 编辑: 结果是< code>WerbDriver不支持类名中的空格,omg。 你们能帮我在这种情况下使用< code>CSS选择器来找到元素吗?

  • 问题内容: 当使用正则表达式按复合类名称搜索时,BeautifulSoup返回空列表。 例: 我需要对课程的选择非常精确。有任何想法吗? 问题答案: 不幸的是,当您尝试对包含多个类的类属性值进行正则表达式匹配时,会将正则表达式分别应用于每个单个类。 这一切都是因为是一个很特别的多值属性,每一次你解析HTML的一个的树建设者(取决于解析器选择)内部分裂从一个类的字符串值入类(报价列表的docstri

  • 我正在尝试使用以下模式: 这对我来说似乎很简单,但是我收到以下错误: 索引签名参数类型不能是联合类型。请考虑改用映射的对象类型。 我做错了什么?

  • 我试图得到以下每个元素使用 inspect元素的输出如下。 但是它失败了,并抛出下面的错误。 另外,我如何遍历每个类?每个类都包含子部分,我想在进入下一个类之前进一步单独处理这些子部分。

  • Spring Data Neo4j是否支持匿名类的持久性?我设法持久化了一个匿名类的对象,但是当试图创建索引(通过注释)时,出现了问题——在SDN 2.3.4中。释放我不能得到唯一的索引工作,在3.0.0.释放我得到了一个错误: 我有一个抽象的用户类,它是Person和Company的子类。但是在创建fresh数据库时,我想创建一个管理员用户,对于这个用户,我认为(可能是错误的)不需要创建不同的域