2017-12-18 208 views
0

我想selenium webdriver点击这个链接,所以我可以继续我的项目。这是我的代码。没有错误,但没有点击链接。Selenium不会点击,无名称,ID或类

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

下面是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> 

我想点击链接第七。我从Chrome的检测工具中直接复制了xpath。我知道一个绝对的xpath是不可靠的,但是当没有类名,id或者名字时我还有什么可用的?帮助将不胜感激。

+0

可以共享URL? –

+0

我已添加网址 – Cameron

+0

该网址似乎仍然不是描述中,请添加它,这样我们可以帮助进一步 – Danny

回答

0

尝试以下XPath:在<a>标签

"//a[contains(@src, 'PREP')]" 
+0

感谢您的帮助。不幸的是,这会导致错误:没有这样的元素。这与我尝试find_element_by_link_text时发生的错误相同。通过我的IDE查看源代码时链接不可点击:print(driver.page_source)。这可能是问题吗? – Cameron

1

点击可能无法获取您所需的输出。你需要去更深,直到<img>标签并调用click()如下:

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

Note : We could have done away with a shorter value for src attribute as PREP but ideally we should consider the full value of the attribute.

+0

谢谢你的帮助。不幸的是,这会导致错误:没有这样的元素。这与我尝试find_element_by_link_text时发生的错误相同。通过我的IDE查看源代码时链接不可点击:print(driver.page_source)。这可能是问题吗? – Cameron

+0

@Cameron你能否详细说明链接是什么意思是不可点击的。请用一些更多的outerHTML来更新这个问题。 – DebanjanB