java InvalidSelectorError:不允许复合类名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28518566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:44:27  来源:igfitidea点击:

InvalidSelectorError: Compound class names not permitted

javaseleniumxpathselenium-webdriverweb-scraping

提问by Zack

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

在此处输入图片说明

采纳答案by Saifur

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

如果不需要,我不会那么担心班级名称。我会使用 css 选择器。

.code-list>div

.code-list>div

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

在CSS公告.意味着类,所以我指着带班格code-list>div它允许我们选择所有子格

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

您还可以使用:nth-child()函数来获取具有索引号的特定子 div

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

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

The above css allows you to select the first child div

上面的css允许你选择第一个子div

As per your screenshot

根据你的截图

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

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

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

一个代码块,可以帮助 OP 理解应该如何处理这种情况

//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();
}