我有这样的html:
<div class="card">
<div>Foo</div>
<a>View Item</a>
</div>
<div class="card">
<div>Bar</div>
<a>View Item</a>
</div>
我想选择卡片匹配“栏”并点击“查看项目”链接。我试过了
cards = browser.find_elements_by_class_name('card')
for card in cards:
if card.find_element_by_partial_link_text('Bar'):
item_anchor = card.find_element_by_partial_link_text('View Item')
item_anchor.click()
但是我得到了一个错误:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"partial link text","selector":"Bar"}
如果要单击
Bar
的View Item
,可以直接使用以下xpath:不过,引入webdriver wait对于稳定性来说是个不错的主意,正如@supputuri所提到的那样
尝试使用EC和下面的xpath。你知道吗
选项1:
检查链接是否存在,然后单击(如果要查找任何特定链接,可以在xpath中添加属性到link)
选项2:
使用不同的xpath和len
方案3:
如果您不确定在
B
和A
之间是否存在任何级别,可以使用下面的xpath。你知道吗相关问题 更多 >
编程相关推荐