selenium-Python帮助,无法通过xpath找到元素

时间:2017-06-13 02:04:12

标签: python selenium xpath

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找到任何元素。

对此的任何想法?

1 个答案:

答案 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']
相关问题