2016-09-14 59 views
0

我正在嘗試在C#中創建適當的XPATH語法,以便從Amazon購物車屏幕中單擊刪除按鈕。購物車中可能有一件物品或多件物品,因此我正在尋找循環刪除按鈕。我無法通過僅包含單詞Delete來找到Delete按鈕。我終於說好了讓我找到屏幕上的所有按鈕。我將遍歷它們並使用每個鏈接中的文本來確定我需要單擊哪一個。唯一的問題是,它確實在頁面上找到了20個不同的項目((「// input [@ type ='submit']」)),但Text域全部爲空。我對這個XPATH語法很陌生,它確實給我帶來了問題。任何幫助,將不勝感激。C#Selenium循環通過Span類中的輸入按鈕XPATH

這裏有一些的很多東西我用的相關信息的屏幕截圖試圖沿:

var links = driver.FindElements(By.XPath("//table[contains(@name, 'ReportViewer_fixedTable')]")); 
var links = driver.FindElements(By.XPath("//css=a[name^='submit.delete']")); 
var links = driver.FindElement(By.CssSelector("table[id*='ReportViewer_fixedTable']")); 
var links = driver.FindElements(By.XPath("//button[@type='submit'][text()='Delete']")); WORKED DIDNT FIND ANYTHING 
var links = driver.FindElements(By.XPath("//input[@type='submit'][contains(text(),'Delete')]")); //WOREKED DIDNT FIND ANYTHING 
var links = driver.FindElements(By.XPath("//input[@type='submit']")); 
var links = driver.FindElements(By.XPath("//div[@class='a-row sc-action-links']/span[@class='a-declarative']")); 

檢驗從鉻: enter image description here

,哪些是我的輸出看起來就像當我通過公正環找到//輸入[@ type ='submit']。你可以看到文字是空的。對他們來說就是這樣。

enter image description here

+0

是否要單擊任何特定的刪除按鈕或獲取所有按鈕的值? –

+0

//input[starts-with(@name,"submit.delete「)] – derloopkat

回答

1

xpath text()input與慾望文本Delete沒有子元素的上下文節點的匹配文本節點孩子。

其實Delete文字出現在輸入元素的value屬性,所以你應該儘量配合Delete文字與價值屬性爲@value = 'Delete'

因此,要找到所有使用Xpath嘗試下面的按鈕刪除: -

var deleteButtons = driver.FindElements(By.XPath("//input[@type='submit'][@value='Delete']")); 

或如下使用CssSelector: -

var deleteButtons = driver.FindElements(By.CssSelector("input[type='submit'][value='Delete']")); 

UPDATE: 對於任何可能需要這在未來。這是成品:

var deleteButtons = driver.FindElements(By.CssSelector("input[type='submit'][value='Delete']")); 

     foreach (var link in deleteButtons) 
     { 
      link.Click(); 
     } 
+1

謝謝你,先生。這一個工作就像一個魅力。 var deleteButtons = driver.FindElements(By.CssSelector(「input [type ='submit'] [value ='Delete']」)); – Roro