2017-11-25 124 views
2

所以我尝试点击YouTube上的一个按钮,但我找不到Xpath的按钮,因为有那么多的按钮,所以我尝试将它们保存在IList中,现在我想单击列表中的特定按钮。C#如何点击IList中的IWebelement?

ChromeDriver chrome = new ChromeDriver(); 
List<IWebElement> textfields = new List<IWebElement>(); 
chrome.Navigate().GoToUrl("https://www.youtube.com/watch?v=9bZkp7q19f0"); 
textfields = chrome.FindElements(By.XPath("//*[@id=\"button"]")).ToList(); 

回答

1

作为每SeleniumFindElements(By.XPath("//*[@id=\"button"]"))文档将返回一个类型ReadOnlyCollection<IWebElement>List。所以你可以简化你的代码,如:

ChromeDriver chrome = new ChromeDriver(); 
List<IWebElement> textfields = new List<IWebElement>(); 
chrome.Navigate().GoToUrl("https://www.youtube.com/watch?v=9bZkp7q19f0"); 
textfields = chrome.FindElements(By.XPath("//*[@id='button']")); 
foreach (IWebElement field in textfields) 
{ 
    string my_text = field.GetAttribute("any_attribute_button_tag"); 
    Console.WriteLine(my_text); 
}