2016-11-16 30 views
-1

我想用WatiN在文本中查找所有字符串。 我可以找到第一個字符串,但我無法收集所有的字符串。 例如,我想在本文中查找「水」的所有匹配項:幾次在文本中查找一個字符串並每次都執行操作

地球上有很多水。但並非所有的水都可以飲用。

所以在這裏我應該找到2個「水」。我的代碼只找到第一個。我怎麼能找到他們兩個?

我的代碼是:

IE ie = new IE("http://examplesite.com"); 

Element test = ie.Table(Find.ByClass("dataTable")).Element("td"); 

ie.TableRow(Find.ByText(t => t.Contains("example string"))).TextField(Find.ByName("examplename")).Button(Find.ByValue("example value")).Click(); 

ie.WaitForComplete(); 
Console.WriteLine("finished"); 

回答

1

可以使用的IndexOf在循環中找到一個子多次出現。

const string haystack = "there are lots of water in the earth. but all of water arent drinkble."; 
const string needle = "water"; 

var startIndex = -1; 
while ((startIndex = haystack.IndexOf(needle, startIndex + 1)) > -1) 
{ 
    Console.WriteLine("Found {0} at {1}", needle, startIndex); 
} 
0

拆分字符串,然後抓住所有包含您要查找的條目。

var sentence = "There is a lot of water on earth. But not all water is drinkable." 
var parts = sentence.Split(' '); 
var words = parts.Where(p => p == "water"); 
+0

或者只是做'parts.Count(p => p ==「water」)'。列出所有項目都是「水」的列表似乎不是很有用。 – Berend

+0

原來的問題很模糊,他爲什麼要這樣做。我不知道什麼對他有用或沒有。 –

+0

是的,我認爲它不是有用的(沒有嘗試我們的代碼),我想找到幾個具有相同ID名稱的元素。並改變所有的價值。例如我想找到所有帶有id的文本框「test」,並將thire的值改爲其他值。 – behzad

相關問題