5

I know that selenium can use css locators.

I know that the syntax is something like this:

xpath=//div[@id,'topLeft')//span[contains(@class,'name')]
or css=#topLeft .name

Now, what if .name is like this: //span[contains(@class,'name with space')]

Then it would fail... HOw to look for a locator that has space in it?

Thanks!

EDIT Solution: css=span.name.with.space

2 Answers 2

5

Class names can't have spaces. However, you can define multiple classes for a single element by putting a space between them. Take a look at the id and class identifiers section in the HTML spec for more.

You should be able to use the CSS locator by using only one of the classes.

If that doesn't work, double check your CSS selector using a tool like Firefinder for FireBug. I was able use Selenium-IDE with an element that had two classes. For the HTML

<div class="c1 c2">
    <span class"s1">Test</span>
</div>

I used the selector

css=div.c1 span
Sign up to request clarification or add additional context in comments.

6 Comments

Yeah. Say that to the devs at my company who have class names like this: button AddComment IntegrationButton.
I'm not saying there can't be spaces. I'm pointing out that those aren't all one class. Those are individual classes. If you're trying to identify one with a selector, just pick a class.
Now i'm the one who is dumb... I never thought that an element can have multiple classes? Damn... I learn something new everyday. :) That means if i see a class name like this: "button Something submitter"... That that element has button class Something class and submitter class? And if i search for button class then this one will also be found?
But what if there are more c1 and c2 around alone but only one element with booth classes? And i need THAT element?
I can simply use + to add class names. Thanks. :)
|
-2

With CSS its easier than using a long xpath:

span.class1.class2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.