nth-of-type和nth-child区别

<pre><code >for i in range(len(items)):    

    item = self.driver.find_element(By.CSS_SELECTOR, f"div.post-list article:nth-of-type({i + 1}) a.post-item-title")</code></pre><p>CSS定位比xpath效率更高,使用起来确实更方便。</p><p>div article:nth-child(1) 和 div article :nth-child(1)是有区别的。</p><p>也就是nth-child的冒号前面有无空格,含义是不一样的。</p><p><br></p><pre><code >&lt;div&gt;

   &lt;span&gt;&lt;/span&gt;

   &lt;article&gt;&lt;/article&gt;

   &lt;article&gt;&lt;/article&gt;

   &lt;article&gt;&lt;/article&gt;

&lt;/div&gt;

</code></pre><p>对于上例: div article:nth-child(1) 会提示找不到,如果要定位到第一个article元素,要采用</p><p>"div article:nth-of-type(1)"</p><p><br></p>

posted on 2022-08-27 20:34  秀泉  阅读(15)  评论(0编辑  收藏  举报

导航