html如下:
<td>
<a href="JavaScript: void(0);" onclick="submitAction(document.bfcm003s, 'bfcm003s204'); return false;">
<img src="../images/menu_buttons/sub_bnyushu04_n.jpg" alt="search by NO" width="131" height="34" border="0">
</a>
</td>
我的python代码:
imgs = driver.find_element_by_xpath('//a[img/@src="../images/menu_buttonssub_bnyushu04_n.jpg"]')
但它说代码无法通过此xpath找到任何元素。
对此的任何想法?
答案 0 :(得分:1)
您可以尝试 xpath :
//a/img[@src="../images/menu_buttons/sub_bnyushu04_n.jpg"]
使用lxml
进行测试:
from lxml import etree
tree = etree.fromstring("""<td>
<a href="JavaScript: void(0);" onclick="submitAction(document.bfcm003s, 'bfcm003s204'); return false;">
<img src="../images/menu_buttons/sub_bnyushu04_n.jpg" alt="search by NO" width="131" height="34" border="0"/>
</a>
</td>""")
# extract the width attribute of the img node
tree.xpath('//a/img[@src="../images/menu_buttons/sub_bnyushu04_n.jpg"]/@width')
# ['131']