6

I am using driver.findelement by.classname method to read an element on firefox browser but i am getting "Compound class names are not supported. Consider searching for one class name and filtering the results." exception

here is my code

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>
Yi Zeng
  • 32,020
  • 13
  • 97
  • 125
Rahul Lodha
  • 3,601
  • 7
  • 25
  • 34

2 Answers2

23

No, your own answer isn't the best one in terms of your question.

Imagine you have HTML like this:

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

driver.FindElement(By.ClassName("bighead")) will find both and return you the first div, instead of the one your want. What you really want is something like driver.FindElement(By.ClassName("bighead crb")), but like you said in your question, this won't work as you need another way to find elements by compound class names.

This why most people use more powerful By.CssSelector or By.XPath. Then you have:

CssSelector (the best):

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 (the better):

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
Yi Zeng
  • 32,020
  • 13
  • 97
  • 125
  • 1
    CSS Selectors are definitely the way to go on this one. They are very powerful. – Derek W Dec 04 '13 at 03:07
  • @user1177636 thank you for this example. I will surely implement it in my automated test scripts soon. – Rahul Lodha Dec 04 '13 at 03:48
  • i tried to implement both CssSelector and XPath. For some reason CssSelector is not able to find the element, but XPath works. I think i will use XPath going forward. Thank You for your help! :) – Rahul Lodha Dec 04 '13 at 16:53
2

Figured out this problem, you have to search by:

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

any space in html class represents a new class name so just search by the first word.

PUG
  • 4,301
  • 13
  • 73
  • 115
Rahul Lodha
  • 3,601
  • 7
  • 25
  • 34
  • 1
    The better way would be to use CSS selectors, this is what they are there for in the first place. – Arran Dec 03 '13 at 22:04
  • @Arran can you give an example how to use css selector in this situation? i wanna move away from using id and classnames – Rahul Lodha Dec 03 '13 at 22:40