【发布时间】:2020-03-19 00:06:07
【问题描述】:
抓取网站:cbc
背景信息:
Chrome 网络驱动器
python3
- 当前最新版本的 chrome 驱动器和 selenium (11/22/2019)。
我的目标:
从每个 vf-comment-thread 类中提取 cmets。
html的部分结构如下所示:
<div class="vf-commenting vf-comments-widget">
...
<div class="vf-horizontal-list vf3-conversations-list vf3-conversations-list--comments">
<div class="vf-comment-thread"> ... </div>
<div class="vf-comment-thread"> ... </div>
<div class="vf-comment-thread"> ... </div>
...
</div>
...
</div>
问题:当我使用 selenium 定位到课堂时
"vf-horizontal-list vf3-conversations-list vf3-conversations-list--comments"
并将其存储在一个变量中:“comm”,然后打印[i.get_attribute("class") for i in comm.find_elements_by_css_selector("*")]。它应该向我显示一个类似[..., "vf-comment-thread", "vf-comment-thread" , "vf-comment-thread", ...] 的列表但是,我得到了一个空列表。
我的精确命令:
wait = WebDriverWait(self.driver, 14)
comm = wait.until(ec.presence_of_element_located((By.CLASS_NAME, "vf-commenting")))
wait = WebDriverWait(comm, 14)
comms = wait.until(ec.presence_of_element_located((By.XPATH, ".//div[@class = 'vf-horizontal-list']")))
print([i.get_attribute("class") for i in comms.find_elements_by_css_selector("*")])
Output: []
【问题讨论】:
-
".//div[@class= vvf-horizontal-list']" 缺少引号(“v”应该在哪里引用?)...您可能想要包含而不是等于?
-
哦,是的,我只是没有足够仔细地复制它们。我的原始代码是正确的。谢谢你的回答。
标签: python selenium selenium-webdriver css-selectors selenium-chromedriver