java - 无效选择器错误: Compound class names not permitted

标签 java selenium xpath selenium-webdriver web-scraping

我正在尝试使用

获取以下每个元素
element = driver.findElement(By.className("code-list-item code-list-item-public "));

inspect 元素的输出如下。

<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>

但是它失败并抛出以下错误。

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

另外,我如何遍历每个类?其中每个都包含子部分,我想在进入下一个之前进一步单独处理这些子部分。

enter image description here

enter image description here

最佳答案

如果不需要的话,我不会太担心类名。我会使用 css 选择器。

.code-list>div

CSS 中的通知 . 表示类,因此我指向带有类 code-list>div 的 div,它允许我们选择所有子div

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

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

上面的CSS允许您选择第一个子div

根据你的截图

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

可以帮助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();
}

关于java - 无效选择器错误: Compound class names not permitted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28518566/

相关文章:

selenium 2 Web 驱动程序另存为对话框

python - Instagram 关闭 API 服务后,如何使用 python 将照片上传到 Instagram

selenium - 计算与给定xpath表达式匹配的元素数

XPath 2.0 : Retrieving nodes by attribute where value is case Insensitive

java - 十进制格式的模式

java - FileReader,如果文件不存在则创建文件

Java Collections HashMap,值意外变化

java - 如何使用正则表达式在 Eclipse 中定义文件关联的 contentType

selenium - 如何更新 Windows 10 的 Chrome 驱动程序

python - 从 lxml xpath 查询操作列表