为什么我有时会收到警告 "Expected 'collections.Iterable', got 'WebElement' instead"?

Why do I sometimes get the warning "Expected 'collections.Iterable', got 'WebElement' instead"?

使用 Selenium 的 find_element(By.XPATH, "//tag[@class='classname']",当我尝试遍历某些 类 的元素时,有时 Pycharm 会收到此警告:“预期 'collections.Iterable',取而代之的是 'WebElement'”。我不明白为什么它会毫无问题地遍历某些 类 而其他人则不会。会不会跟不同的标签有关?

在下面的代码中,issues_available 以绿色突出显示并显示上述警告。

代码:

issues_available = driver.find_element(By.XPATH, "//div[@class='issue ember-view']")
    currently_clicked_issue = driver.find_element(By.XPATH, "div[@class='issue active-override ember-view']")
    issues_list = []
    for issue in issues_available:

        issue_vol_number = issue.find_element(By.XPATH, ".//span[@class='label']").text
///

当你写这篇文章时

issues_available = driver.find_element(By.XPATH, "//div[@class='issue ember-view']")

现在 issues_available 是单个网络元素。

并且迭代单个 Web 元素毫无意义。

相反,您应该使用:

find_elements

这将 return 网络元素列表。

因此,您的有效代码将是:

issues_available = driver.find_elements(By.XPATH, "//div[@class='issue ember-view']")
currently_clicked_issue = driver.find_element(By.XPATH, "div[@class='issue active-override ember-view']")
issues_list = []
for issue in issues_available:
    issue_vol_number = issue.find_element(By.XPATH, ".//span[@class='label']").text