2011-06-06 38 views
2

我想使用CSS選擇器來點擊Selenium中的鏈接,但它無法識別它。Selenium無法識別頁面上的第二個元素的CSS路徑

稱爲「取消」的鏈接出現在頁面的兩個地方。

第一個取消鏈接的XPath是: //html/body/div[@id='c_account']/div[@id='a_returns']/div[@id='container']/div[@id='main']/div[@id='main_col']/div/div[@id='create-return']/div[1]/div/a[2]/span

第二鏈路的XPath是: /html/body/div[@id='c_account']/div[@id='a_returns']/div[@id='container']/div[@id='main']/div[@id='main_col']/div/div[@id='create-return']/div[4]/div/a[2]/span

當我檢查在Firebug兩個鏈接,我得到了相同的CSS路徑。 html body.p div#c_account.c_wrapper div#a_returns.a_wrapper div#container div#main div#main_col div.main_content div#create-return div.return-process-actions div.return-process-action-buttons a.return-process-cancel

當我使用Selenium點擊第二個鏈接時,它點擊第一個鏈接,因爲這兩個鏈接的css路徑是相同的。

有沒有辦法通過css路徑來區分這兩個鏈接?

回答

0

長遠來看,如果您想要點擊的鏈接添加一個帶有有意義名稱的id,您會更好。當你有一個像

selenium.click("firstCancelLink"); 

線比

selenium.click("css=html body.p div#c_account.c_wrapper div#a_returns.a_wrapper div#container div#main div#main_col div.main_content div#create-return div.return-process-actions div.return-process-action-buttons a.return-process-cancel"); 
+0

是真的,但那不是我正在尋找的解決方案,因爲我無法更改開發人員編寫的代碼。 – user786045 2011-06-06 15:44:24