2013-10-17 25 views
0

好吧,這個讓我在牆上碰撞了一段時間。
我看一個網頁元素的列表。我訪問列表,像這樣在頁面重新載入後查找元素

foreach (IWebElement link in driver.FindElementsByCssSelector("span.cn.mailbox > a")) 
     { 
       // Click at a lot of page and the page will reload eventually 
     } 

的問題是,在循環中,我需要改變的網頁之類的東西,但最後我回到該頁面有鏈接列表。
只要我打第二次迭代,我得到以下錯誤:

enter image description here

可能是因爲我改變了頁面,甚至認爲在收集我循環的鏈接通過是相同的,編譯器似乎並不認爲它是完全相同的集合。

有沒有辦法解決這個問題或我可以使用的解決方法?

回答

0

你得到異常的原因是因爲driver.FindElementsByCssSelector所引用的頁面已被重新加載。

像這樣的東西應該工作: 創建一個鏈接文本數組。遍歷鏈接文本數組,單擊每個鏈接。

string [] links = new string[driver.FindElementsByCssSelector("span.cn.mailbox > a").Count); 

int i = 0; 
foreach (IWebElement link in driver.FindElementsByCssSelector("span.cn.mailbox > a")) 
{ 
    links[i++] = link.Text; 
} 

for (int i = 0; i < driver.FindElementsByCssSelector("span.cn.mailbox > a").Count; i++) 
{ 
    driver.FindElementByLinkText(links[i]).Click(); 
} 
相關問題