C# how to click an IWebelement in a IList?

淺唱寂寞╮ 提交于 2020-01-24 13:35:08

问题


So I tried to click a button on YouTube, but I can't find the button by the Xpath because there are so many buttons, so I tried saving them in an IList, now I want to click a specific button in the 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"]")).ToList();

回答1:


As per the documentation of Selenium FindElements(By.XPath("//*[@id=\"button"]")) will return a List of type ReadOnlyCollection<IWebElement>. So you can simplify your code like :

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);
}


来源:https://stackoverflow.com/questions/47485022/c-sharp-how-to-click-an-iwebelement-in-a-ilist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!