2

I am trying to get each of the below elements using

element = driver.findElement(By.className("code-list-item code-list-item-public "));

The output of inspect element is as follows.

<div class="column one-fourth codesearch-aside"></div>

<div class="column three-fourths codesearch-results">

    <div class="sort-bar"></div>
    <div id="code_search_results">
        <div class="code-list">
            <div class="code-list-item code-list-item-public "></div>
            <div class="code-list-item code-list-item-public "></div>
            <div class="code-list-item code-list-item-public "></div>
            <div class="code-list-item code-list-item-public "></div>
            <div class="code-list-item code-list-item-public "></div>
            <div class="code-list-item code-list-item-public "></div>
            <div class="code-list-item code-list-item-public "></div>
            <div class="code-list-item code-list-item-public "></div>
            <div class="code-list-item code-list-item-public "></div>
            <div class="code-list-item code-list-item-public "></div>
        </div>

But it fails and throws the below error.

Caused by: org.openqa.selenium.InvalidSelectorException: The given selector code-list-item code-list-item-public  is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: Compound class names not permitted
For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html

Also, How do I traverse through each of the classes? Each of these contain subparts which I would like to process further individually before moving to the next.

enter image description here

enter image description here

Zack
  • 2,078
  • 10
  • 33
  • 58
  • 1
    Take a look at the answer to [this question](http://stackoverflow.com/questions/16089492/selenium-webdriver-w-java-locating-elements-with-multiple-class-names-with-one) – Titus Feb 14 '15 at 18:10
  • So, when I try this , element = driver.findElement(By.className("code-list-item")); it works but when I try to use "code-list-item code-list-item-public " it does not. – Zack Feb 14 '15 at 19:04
  • Probably because `By.className(...)` doesn't accept multiple class names, if you want that, use the method that I've previously suggested or try to separate the class names with a `,` – Titus Feb 14 '15 at 19:16
  • @Zack the error tells you exactly what's happening. It is a compound class you you use `code-list-item code-list-item-public` combination of two different classes and when you use `code-list-item` it's only one class since it's also unique it works – Saifur Feb 14 '15 at 19:28
  • @Saifur Yes . You are right. I got this. So each of the classes "code-list-item code-list-item-public " is inside the class "code-list". I need to traverse through all the "code-list-item code-list-item-public " classes . How do I do this? – Zack Feb 14 '15 at 19:35
  • Are you trying to fetch all the `div` with compound class class `code-list-item code-list-item-public`? Which is 10 in count? – Saifur Feb 14 '15 at 19:38
  • yes. If you see the image which I just attached, for each of the compound class "code-list-item code-list-item-public" I want to go to href link and click on it and then go to view code changes – Zack Feb 14 '15 at 19:40
  • @Zack I am getting confused with the `html` and the image you provided. Doesn't look same – Saifur Feb 14 '15 at 19:44
  • @Saifur If you expand each of the code-list-item code-list-item-public div classes, you get something that is visible in the image – Zack Feb 14 '15 at 19:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/70928/discussion-between-saifur-and-zack). – Saifur Feb 14 '15 at 19:45
  • Are you sure you really care only about the items with both classes? Just pick one and be happy. Otherwise you would have to get both and intersect the results. – eckes Feb 15 '15 at 01:26
  • 1
    Possible duplicate of [Compound class names are not supported error in WebDriver](http://stackoverflow.com/questions/15699900/compound-class-names-are-not-supported-error-in-webdriver) – Gaurav Lad Aug 31 '16 at 16:01

1 Answers1

2

I wouldn't worry about the class name that much if I don't have to. I would use css selector.

.code-list>div

Notice in css . means class so I am pointing to the div with the class code-list and >div it allows us to select all child div

You also can use :nth-child() function to grab a specific child div with index number

.code-list>div:nth-child(1)

The above css allows you to select the first child div

As per your screenshot

.code-list>div:nth-child(1)>a

A code block that may help OP to understand how this scenario should be handled

//maximizing the window for better view
driver.manage().window().maximize();

//a selector to find all the links on the page
By selector = By.xpath("//p[@class='title']/a[1]");

//finding the list of all elements
List<WebElement> list = driver.findElements(selector);

/*Iterating over the collection may throw StaleElementReference exception due to DOM refresh
according to my knowledge for loop is best in such case
*/
for (int i = 0; i<list.size(); i++){

    new WebDriverWait(driver,10).until(ExpectedConditions.elementToBeClickable(selector));

    //Click on the title
    driver.findElements(selector).get(i).click();

    //Navigating back to the main page. This is not feasible but no other option present due to page structure
    driver.navigate().back();
}
Saifur
  • 16,081
  • 6
  • 49
  • 73