InvalidSelectorError: Compound class names not permitted
InvalidSelectorError: Compound class names not permitted
我正在尝试使用
获取以下每个元素
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
此外,如何遍历每个 类?每一个都包含子部分,我想在进入下一个之前进一步单独处理。
如果不需要的话,我不会那么担心 class 这个名字。我会用 css select 或者.
.code-list>div
注意 css .
表示 class 所以我用 class code-list
和 [=14 指向 div =] 它允许我们 select 所有 child div
您也可以使用 :nth-child()
函数来获取索引号为
的特定 child div
.code-list>div:nth-child(1)
以上css让你select第一个childdiv
根据你的截图
.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();
}
我正在尝试使用
获取以下每个元素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
此外,如何遍历每个 类?每一个都包含子部分,我想在进入下一个之前进一步单独处理。
如果不需要的话,我不会那么担心 class 这个名字。我会用 css select 或者.
.code-list>div
注意 css .
表示 class 所以我用 class code-list
和 [=14 指向 div =] 它允许我们 select 所有 child div
您也可以使用 :nth-child()
函数来获取索引号为
.code-list>div:nth-child(1)
以上css让你select第一个childdiv
根据你的截图
.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();
}