我使用通过使用nth-child(n)来定位元素。
下面是我的html代码:
<div id="iopop" style="">
<div style="" class="">
<div id="iopoph" class="animated zoomIn" style=" ">
<span style="" class="gs_hover"></span>
<b class="in">FALAFEL</b>
<a iid="128-73" class="itemsub lowend" price="2.99" name="FALAFEL (6)" style="">
<b class="in">(6)</b>
<b class="is"></b>
<b class="ip">2.99</b>
<b class="iq"></b></a>
<a iid="128-74" class="itemsub lowend" price="4.99" name="FALAFEL (12)" style="">
<b class="in">(12)</b>
<b class="is"></b>
<b class="ip">4.99</b>
<b class="iq"></b>
</a>
<b class="is"></b>
<b class="ip"></b>
<b class="iq"></b>
</div>
</div>
</div>现在,我想通过使用nth-child(n)来定位第一个a标记,所以我尝试:
driver.find_element_by_css_selector('div#iopoph a:nth-child(2)').click()但是有一个错误说:
NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"div#iopoph a:nth-child(2)"}
(Session info: chrome=79.0.3945.88)有朋友能帮忙吗?
发布于 2020-01-02 11:22:13
你很亲密,但你需要考虑几件事:
div#iopoph将标识父节点,即- Locating `<a iid="128-73" class="itemsub lowend" price="2.99" name="FALAFEL (6)" style="">`:div.animated.zoomIn#iopoph a:nth-of-type(1)
-定位<a iid="128-74" class="itemsub lowend" price="4.99" name="FALAFEL (12)" style="">:
div.animated.zoomIn#iopoph a:第n种类型(2)
发布于 2020-01-02 15:27:42
您可以使用这个定位器a:nth-of-type(2)
发布于 2020-10-12 12:29:02
在你的例子中-
div#iopop a:nth-child(3) -> returns first a <anchor> tag i.e. <a iid="128-73"
div#iopop a:nth-child(4) -> returns second a <anchor> tag i.e. <a iid="128-74"
nth-child(index):
第n-子(索引)方法用于选择/获取指定的索引子元素。但是指定的索引元素应该与冒号(:)前面提到的相同。
在你的情况下-
div#iopoph a:nth-child(2)->它指向标记,它与前面提到的标记不一样:(冒号)。因此,它返回非解析。
https://stackoverflow.com/questions/59548700
复制相似问题