C# 不支持复合类名称。考虑搜索一个类名并过滤结果 < >我使用Dr.FrimeBy.Cype方法读取Firefox浏览器上的一个元素,但我得到“复合类名称不支持。考虑搜索一个类名并过滤结果”。

C# 不支持复合类名称。考虑搜索一个类名并过滤结果 < >我使用Dr.FrimeBy.Cype方法读取Firefox浏览器上的一个元素,但我得到“复合类名称不支持。考虑搜索一个类名并过滤结果”。,c#,selenium,webdriver,selenium-webdriver,C#,Selenium,Webdriver,Selenium Webdriver,这是我的密码 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;"

这是我的密码

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>
driver.FindElement(By.ClassName(“bighead crb”)).Text.Trim().ToString()
//下面是浏览器的html的外观
铅交付变得容易

解决了这个问题,您必须通过以下方式进行搜索:

driver.FindElement(By.ClassName("bighead")).Text.Trim().ToString(); //instead of 
driver.FindElement(By.ClassName("bighead crb")).Text.Trim().ToString();

html类中的任何空格都代表一个新的类名,所以只需按第一个单词搜索即可。

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

假设您有如下HTML:

<div class="bighead ght">LEAD DELIVERY MADE HARD</div>
<div class="bighead crb">LEAD DELIVERY MADE EASY</div>
XPath(更好):


更好的方法是使用CSS选择器,这就是它们的初衷。@Arran您能举个例子说明在这种情况下如何使用CSS选择器吗?我不想使用id和classnamesCSS选择器,这绝对是一个不错的选择。它们非常强大。@user1177636谢谢您的示例。我肯定会很快在我的自动测试脚本中实现它。我尝试实现CssSelector和XPath。由于某些原因,CssSelector无法找到元素,但XPath可以工作。我想我会继续使用XPath。谢谢你的帮助!:)
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
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