Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#如何在IList中单击IWebelement?_C#_List_Selenium_Click - Fatal编程技术网

C#如何在IList中单击IWebelement?

C#如何在IList中单击IWebelement?,c#,list,selenium,click,C#,List,Selenium,Click,所以我试着点击YouTube上的一个按钮,但是我找不到Xpath旁边的按钮,因为有太多的按钮,所以我试着将它们保存在IList中,现在我想点击列表中的一个特定按钮 ChromeDriver chrome = new ChromeDriver(); List<IWebElement> textfields = new List<IWebElement>(); chrome.Navigate().GoToUrl("https://www.youtube.com/watch?v

所以我试着点击YouTube上的一个按钮,但是我找不到Xpath旁边的按钮,因为有太多的按钮,所以我试着将它们保存在IList中,现在我想点击列表中的一个特定按钮

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();
ChromeDriver chrome=新的ChromeDriver();
List textfields=新列表();
chrome.Navigate().gotour(“https://www.youtube.com/watch?v=9bZkp7q19f0");
textfields=chrome.FindElements(By.XPath(“/*[@id=\”button“])).ToList();

根据
findelelements(By.XPath(“/*[@id=\“button”]”)的文档,
将返回类型为
只读集合的
列表。因此,您可以简化代码,如:

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);
}
ChromeDriver chrome=新的ChromeDriver();
List textfields=新列表();
chrome.Navigate().gotour(“https://www.youtube.com/watch?v=9bZkp7q19f0");
textfields=chrome.FindElements(By.XPath(“/*[@id='button']);
foreach(textfields中的IWebElement字段)
{
字符串my_text=field.GetAttribute(“任意_属性_按钮_标记”);
Console.WriteLine(我的文本);
}