【发布时间】:2017-11-24 21:39:05
【问题描述】:
在this 网站上,我们可以看到图表下方的描述(例如:“Mohammad Nawaz to Eoin Morgan, 55.0 mph, dot ball.”)反复变化。 我想刮掉关于轨迹的所有描述。 在 HTML 中,关于描述的具体部分是:
<div class="trajDescription" id="traj-description" classname="trajDescription">
<b>34.3</b>
Mohammad Nawaz to Eoin Morgan, 55.0 mph, dot ball.
</div>
当我使用 selenium 定位元素并想要获取文本时:
traj_description = driver.find_element_by_css_selector('#traj-description')
print(traj_description.text)
我得到的结果总是一句话。喜欢:
40.1 Chris Woakes to Sarfraz Ahmed, 84.2 mph, 1 run, hit straight down the ground.
有什么方法可以获取所有的描述吗?例如:
Mohammad Nawaz to Eoin Morgan, 55.0 mph, dot ball.
Mohammad Nawaz to Eoin Morgan, 45.0 mph, down to the ground.
Chris Woakes to Sarfraz Ahmed, 84.2 mph, 1 run, hit straight down the ground
【问题讨论】:
-
可能不会,因为所有内容并不同时存在于 DOM 中。它看起来是通过 JS 动态更改的。你只是得到初始值。
-
尝试执行此操作的代码在哪里?您发布的代码只会打印第一个文本实例。
-
它会工作得很好......你只需要编写等待文本更改的代码,提取文本,并根据需要存储/写入它。
标签: javascript jquery html css selenium