gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:56:38 26 4
gpt4 key购买 nike

我正在尝试使用

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

inspect element的输出如下。

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com