简体   繁体   English

CSS dt 元素下所有 dd 元素的选择器

[英]CSS Selector for all dd elements under a dt element

Problem问题

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>

What I've tried我试过的

  1. dt:contains('Life Cycle:') + dd

This only returns the first dd element这只返回第一个dd元素

  1. 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

  1. I wish I could use n-child like 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的子元素。

What I need我需要的

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM