python - 终端无数据打印
问题描述
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("https://www.earningswhispers.com")
element_calendar = driver.find_element_by_id("calendar")
element_calendar.click()
data = driver.find_elements_by_class_name('cor amc showconf nwh')
for d in data:
title = d.find_element_by_xpath('./html/body/form/div[3]/section/ul/li[26]/div[1]/div[3]').text
estimate1 = d.find_element_by_xpath('./html/body/form/div[3]/section/ul/li[26]/div[1]/div[6]').text
estimate2 = d.find_element_by_xpath('./html/body/form/div[3]/section/ul/li[26]/div[1]/div[7]').text
print(title, estimate1, estimate2)
assert "No results found." not in driver.page_source
driver.quit()
解决方案
看起来您的所有定位器都不正确。尝试以下操作:
data = driver.find_elements_by_css_selector('li.cor.amc.showconf.nwh')
for d in data:
title = d.find_element_by_xpath('./div/div[3]').text
estimate1 = d.find_element_by_xpath('./div/div[6]').text
estimate2 = d.find_element_by_xpath('./div/div[7]').text
print(title, estimate1, estimate2)
印刷:
CRM $0.88 $5.89 B
ULTA $1.92 $1.62 B
BOX $0.17 $200.48 M
GPS ($0.02) $3.40 B
OLLI $0.66 $420.52 M
DELL $1.62 $23.36 B
GES ($0.20) $497.31 M
DOMO ($0.43) $57.49 M
FANH Rev: $0.25
LGF.A ($0.17) -
仅供参考,通常在名称中有空格时查找元素class name
是行不通的。在这种情况下使用会更好css selector
。
此外,当您循环遍历一组元素时,您不会在每个循环中一直返回到根 HTML 元素,您的根从您正在循环的元素开始。
推荐阅读
- cookies - Chrome 或任何其他浏览器如何使用公共后缀列表?
- python - 熊猫使用数据框中的两列过滤器为变量赋值
- javascript - 三个js点击HTML叠加后如何取消鼠标点击
- javascript - 在 Bootstrap 中从列表中选择多个项目
- python-3.x - 如何裁剪矩形或正方形内的图像区域?
- swift - Realm Swift - JSON 中的嵌套列表
- r - 我正在尝试使用 xlsx 包将命名范围从 Excel 工作表带到 R
- visual-studio - 如何限制打开文档的数量并自动关闭 Visual Studio 中最旧的文档?
- gnuplot - 使用 gnuplot 拟合小值时遇到问题
- javascript - Javascript 中的四舍五入问题