Python 选择第二个标记

Python 选择第二个标记,python,selenium,css-selectors,Python,Selenium,Css Selectors,如何从以下代码段中选择第二个标记 <div class="hovno"> <a href='...'></a> <a href='...'></a> </div> 但是我不知道如何选择第二个标记。您应该使用n类型的 driver.FindElement(By.CssSelector("div.hovno a:nth-of-type(2)"); 我不确定,但是试试这个 驱动程序。通过css选择器(“div.

如何从以下代码段中选择第二个
标记

<div class="hovno">
    <a href='...'></a>
    <a href='...'></a>
</div>

但是我不知道如何选择第二个
标记。

您应该使用
n类型的

driver.FindElement(By.CssSelector("div.hovno a:nth-of-type(2)");

我不确定,但是试试这个


驱动程序。通过css选择器(“div.hovno”)查找元素。通过标签名称(“a”)查找元素[2]

您始终可以找到所有直接
a
子元素并获取第二个元素:

driver.find_elements_by_css_selector("div.hovno > a")[1]
或者,根据示例,最后一个元素也可以工作:

driver.find_elements_by_css_selector("div.hovno > a")[-1]

伪类也是一个选项:

driver.find_element_by_css_selector("div.hovno > a:nth-of-type(2)")
driver.find_element_by_css_selector("div.hovno > a:nth-of-type(2)")