Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compound class names are not supported error in WebDriver

I have a method to count the number of elements in divs and to return their number.

 public int getNumberOfOpenBets() {

     openBetsSlip = driver.findElement(By.id("form_open_bets"));
     openBets = openBetsSlip.findElements(By.className(" cashout_noCash"));
     return openBets.size();
 }

That's the page source

<form id="form_open_bets" method="post" name="form_open_bets">
    <input type="hidden" value="" name="action">
    <input type="hidden" value="" name="bet_id">
    <input type="hidden" value="" name="cashout_price">
    <input id="target_page" type="hidden" value="" name="target_page">
    <div id="By.id" class="slipWrapper ">
        <div id="openBets_header"></div>
        <div id="cashout_1626" class=" cashout_noCash">
            <div id="cashout_1625" class=" cashout_noCash">
                <div id="cashout_1615" class=" cashout_noCash">
                    <div id="cashout_1614" class=" cashout_noCash">
                        <div id="cashout_1613" class=" cashout_noCash">
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>

WebDriver is throwing the following error: Compound class names are not supported. Consider searching for one class name and filtering the results or use CSS selectors.

org.openqa.selenium.InvalidSelectorException: Compound class names are not supported. Consider searching for one class name and filtering the results or use CSS selectors.
For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html
Build info: version: '2.31.0', revision: '1bd294d185a80fa4206dfeab80ba773c04ac33c0', time: '2013-02-27 13:51:26'
System info: os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.7.0_17'
Driver info: driver.version: unknown
    at org.openqa.selenium.By.className(By.java:131)
    at elements.betslip.Betslip.getNumberOfOpenBets(Betslip.java:136)
    at testSomething(SomethingTest.java:117)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

EDIT:

As it turned out WerbDriver doesn't support spaces in the class names, omg.

Could you guys please help me to use CSS selector in this situation in order to find the elements?

like image 677
Atanas Kanchev Avatar asked Mar 29 '13 08:03

Atanas Kanchev


People also ask

How to handle Compound class name in selenium?

Selenium does not support BY. CLASS_NAME with compound class..you need to use cssSelector or XPath to find el3 here...

How do you write cssSelector in selenium?

Type “css=input[type='submit']” (locator value) in Selenium IDE. Click on the Find Button. The “Sign in” button will be highlighted, verifying the locator value. Attribute: Used to create the CSS Selector.

What is invalid selector exception in selenium?

The invalid selector error is a WebDriver error that occurs when an element retrieval command is used with an unknown web element selector strategy. The available selector strategies are CSS, link text, partial link text, tag name, and XPath. Any other selector strategy is rejected with this error.


2 Answers

This is exactly as expected. If your class name includes a space, WebDriver will see it as a "compound selector". You can either remove the space in your By.className() locator, which should still find the elements you're looking for; or you can move to finding by CSS selectors, using something like By.cssSelector(".cashout_noCash"), which offer far more flexibility for similar functionality. This is exactly what the exception message says.

like image 94
JimEvans Avatar answered Sep 21 '22 05:09

JimEvans


Here is a Ruby answer if anyone needs it. The conclusion I reached is that some of the solutions that worked for java above either don't work on my machine or don't work for Ruby at all (although I am not sure which is the case).

If the html is:

<a class="button orange-bg" href="http://www.MyCarmelHome.com" target="_blank">
     access web portal
</a>

The format to find this element would be:

logInBtn = driver.find_element(:css, ".button.orange-bg")

I used this because the following wouldn't work:

  1. Replacing the spaces with '.' and finding by css selector (you need to put a period at the front).

  2. Removing the spaces in the compound class name and using the class name locator.

like image 42
schro's cat Avatar answered Sep 23 '22 05:09

schro's cat