[英]CSS Selector for all dd elements under a dt element
Is it possible to grab all dd
elements under a specific dt
element?是否可以抓取特定
dt
元素下的所有dd
元素?
Specifically, I am trying to capture the dd
elements under the dt
element titled ' Life Cycle ' on this page: https://plants.ces.ncsu.edu/plants/abeliophyllum-distichum/具体来说,我正在尝试捕获此页面上标题为“生命周期”的
dt
元素下的dd
元素: https://plants.ces.ncsu.edu/plants/abeliophyllum-distichum/
I want to capture ' Life Cycle ' for other plant pages too, where they could have 1 or more dd
elements under the dt
for ' Life Cycle '.我也想为其他工厂页面捕获“生命周期”,它们在“生命周期”的
dt
下可以有 1 个或多个dd
元素。 So I need a CSS selector that could work for the scenario of 1 or more dd
elements.所以我需要一个 CSS 选择器,它可以用于 1 个或多个
dd
元素的场景。
Here is what the HTML structure looks like:这是 HTML 结构的样子:
<dl>
<span>Attributes:</span>
<dt>Life Cycle:</dt>
<dd><span>Perennial</span></dd>
<dd><span>Woody</span></dd>
<dt>Text:</dt>
<dd><span>Text</span></dd>
<dt>Text:</dt>
<dd><span>Text</span></dd>
<dt>Text:</dt>
<dd><span>Text</span></dd>
<dd><span>Text</span></dd>
</dl>
dt:contains('Life Cycle:') + dd
This only returns the first dd
element这只返回第一个
dd
元素
dt:contains('Life Cycle:') ~ dd
This returns all of the dd
elements after the dt
even dd
s under other dt
s.这将返回
dt
之后的所有dd
元素,甚至是其他dt
下的dd
。
dt:contains('Life Cycle:'):nth-child(-n+2)
, but I can't because dd
elements aren't children of dt
s.dt:contains('Life Cycle:'):nth-child(-n+2)
这样使用 n-child,但我不能,因为dd
元素不是dt
的子元素。I need something that is flexible for the scenario of having 1 or more dd
elements under one dt
.对于一个
dt
下有 1 个或多个dd
元素的场景,我需要一些灵活的东西。 I am not able to modify the HTML, because I am scraping this data point from the website.我无法修改 HTML,因为我正在从网站上抓取这个数据点。
This is what I am trying to capture in my CSS selector:这就是我试图在我的 CSS 选择器中捕获的内容:
Looks :contains
css selecter only works in Selenium. In that case, you need something similar to :not
, you can do the following: Looks
:contains
css 选择器仅适用于 Selenium。在这种情况下,您需要类似于:not
的东西,您可以执行以下操作:
dt:contains('Life Cycle:') ~ dd {
color: red;
}
dt:contains('Life Cycle:') ~ dt ~ dd {
color: black;
}
Edit: If you can't use css or xpath, then in jquery you can do the following:编辑:如果您不能使用 css 或 xpath,那么在 jquery 中您可以执行以下操作:
$("dt:contains('Life Cycle:') ~ dd").css("color", "red");
$("dt:contains('Life Cycle:') ~ dt ~ dd").css("color", "black");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.