Selenium will not click, no name, id or class

时间:2017-12-18 06:05:50

标签: python-3.x selenium-webdriver

I would like selenium webdriver to click this link so I can proceed with my project. Here is my code. No errors, but it does not click the link.

x = driver.find_element_by_xpath('/html/body/table[2]/tbody/tr[2]/td/span/  
a[7]')
x.click()

Here is the HTML

<tr>
  <td width="100%">
    <span style="white-space: nowrap">
      <a href="#">
        <img src="https://staffmate.com/imagessm/OEA_sm_lt.gif" border="0" width="125" height="36">
      </a>
      <a href="#">
        <img src="https://staffmate.com/imagessm/OPOS_sm_lt.gif" border="0" width="125" height="36">
      </a>
      <a href="#">
        <img src="https://staffmate.com/imagessm/OCTR_sm_lt.gif" border="0" width="125" height="36">
      </a>
      <a href="#">
        <img src="https://staffmate.com/imagessm/OCL_sm_lt.gif" border="0" width="125" height="36">
      </a>
      <a href="#">
        <img src="https://staffmate.com/imagessm/OVEN_sm_lt.gif" border="0" width="125" height="36">
      </a>
      <a href="#">
        <img src="https://staffmate.com/imagessm/OEV_sm_dk.gif" border="0" width="125" height="36">
      </a>
      <a href="eprintreports.php?eSessID=199495633810875522501415473608154261624185901388039708769020171218111124">
        <img src="https://staffmate.com/imagessm/PREP_sm_lt.gif" border="0" width="125" height="36">
      </a>
      </span>
    </td>
</tr>

I want to click the 7th link. I copied the xpath straight from Chrome's inspection tool. I'm aware that an absolute xpath is unreliable but what else am I to use when there are no class names, id's or names? Help would be much appreciated.

2 个答案:

答案 0 :(得分:1)

点击<a>标记可能无法获取所需的输出。您需要深入<img>标记并按如下方式调用click()

driver.find_element_by_xpath("//td/span//a/img[contains(@src,'https://staffmate.com/imagessm/PREP_sm_lt.gif')]").click()
  

注意:我们可以将src属性的较短值取消为 PREP 但理想情况下我们应该考虑完整value 1}}属性。

答案 1 :(得分:0)

Try the following xpath:

AT^STGI=16