2017-09-14 63 views
-3

我需要創建一個簡單的循環遍歷列表並搜索特定的文本。當找到文本時,它應該向下滾動到該選項,以便它可見,然後單擊它。我想出了這樣的事情,但它在Java中,我需要在C#代碼,任何一個可以幫助我的語法,因爲我剛剛學習c#硒迭代列表C#

public static IWebElement FindListItem(IWebElement listContainer, string itemText) 
{ 
    List<IWebElement> allOptions = listContainer.FindElements(By.XPath("")); 
    for (WebElement we: allOptions) 
    { 
     listContainer.sendKeys(Keys.DOWN); 
     sleep(250); 
     if (we.getText().contains(text)) select.selectByVisibleText("Value1"); 
    } 
} 

回答

0

請嘗試,

public static IWebElement FindListItem(IWebElement listContainer, string itemText) 
    { 

     List<IWebElement> allOptions = listContainer.FindElements(By.XPath("")).ToList(); 
     foreach (var webElement in allOptions) 
     { 
      listContainer.SendKeys(Keys.Down); 
      Thread.Sleep(250); 
      if (webElement.Text.Contains(itemText)) 
       select.selectByVisibleText("Value1"); 
     } 
    } 
+0

.getText有下劃線用消息「IWebElement不包含定義爲‘的getText’和沒有擴展方法‘的getText’接受型的第一參數‘IWebElement’可以發現」 任何想法我怎麼能解決這個問題? –

+0

請嘗試webElement.Text並讓我知道。 –

+0

不可使用的成員'IWebElemnent.Text'不能像方法一樣使用 –

0

這個怎麼樣?

public static void FindListItem(SelectElement select, string itemText) 
{ 
var options = select.Options(); 
if (options.Any(o => o.Text.Contains(itemText)) 
{ 
    select.SelectByIndex(options.IndexOf(options.First(o => o.Text.Contains(itemText)))); 
} 
} 

,將檢查是否有包含該項目的文本的選項,如果是的話那麼它會選擇所述元素的索引。

對不起,如果有任何錯誤,我從我的手機這樣做。

我希望它有幫助。