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