有什么方法可以从 find_elements_by_class 获取图像 "src" 它只是全部返回 None
Is there any possible way to get image "src" from find_elements_by_class it just all returning None
我总共得到 250 个结果。
我试图获取第一个结果的图像 src 中的 10 个,但全部 return None。
如果我切换到使用 XPATH,则无法自动完成,因为它们太多而无法手动选择。
historylist = WebDriverWait(driver, 100).until(
EC.presence_of_all_elements_located((By.CLASS_NAME, "coinflip-list__item.coinflip-history-list__item")))
print(f"Total {len(historylist)}")
for x in range(10):
print(historylist[x].get_attribute("src"))
共250个
打印出前 10 个:
这就是输出。
看来您对 By.CLASS_NAME
的使用不正确。
这里
historylist = WebDriverWait(driver, 100).until(
EC.presence_of_all_elements_located((By.CLASS_NAME, "coinflip-list__item.coinflip-history-list__item")))
CLASS_NAME
或 find_element_by_class_name
或 find_elements_by_class_name
只接受一个 class。化合物 classes 不工作。
改为By.CSS_SELECTOR
:
historylist = WebDriverWait(driver, 100).until(
EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".coinflip-list__item.coinflip-history-list__item")))
其中 coinflip-list__item
和 coinflip-history-list__item
是单个 html 标签中的 class 名称。
在此处查看说明:https://selenium-python.readthedocs.io/locating-elements.html#locating-elements-by-class-name。
第二个,尝试使用visibility_of_all_elements_located
而不是presence_of_all_elements_located
。
除此之外,请检查您的定位器是否正确。
我总共得到 250 个结果。 我试图获取第一个结果的图像 src 中的 10 个,但全部 return None。 如果我切换到使用 XPATH,则无法自动完成,因为它们太多而无法手动选择。
historylist = WebDriverWait(driver, 100).until(
EC.presence_of_all_elements_located((By.CLASS_NAME, "coinflip-list__item.coinflip-history-list__item")))
print(f"Total {len(historylist)}")
for x in range(10):
print(historylist[x].get_attribute("src"))
共250个
打印出前 10 个:
这就是输出。
看来您对 By.CLASS_NAME
的使用不正确。
这里
historylist = WebDriverWait(driver, 100).until(
EC.presence_of_all_elements_located((By.CLASS_NAME, "coinflip-list__item.coinflip-history-list__item")))
CLASS_NAME
或 find_element_by_class_name
或 find_elements_by_class_name
只接受一个 class。化合物 classes 不工作。
改为By.CSS_SELECTOR
:
historylist = WebDriverWait(driver, 100).until(
EC.presence_of_all_elements_located((By.CSS_SELECTOR, ".coinflip-list__item.coinflip-history-list__item")))
其中 coinflip-list__item
和 coinflip-history-list__item
是单个 html 标签中的 class 名称。
在此处查看说明:https://selenium-python.readthedocs.io/locating-elements.html#locating-elements-by-class-name。
第二个,尝试使用visibility_of_all_elements_located
而不是presence_of_all_elements_located
。
除此之外,请检查您的定位器是否正确。