2017-12-18 210 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