C#如何单击IList中的IWebelement? [英] C# how to click an IWebelement in a IList?
本文介绍了C#如何单击IList中的IWebelement?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因此,我尝试单击YouTube上的按钮,但由于Xpath太多,因此无法通过Xpath找到该按钮,因此我尝试将它们保存到IList中,现在我想单击列表.
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();
推荐答案
根据 将返回类型为的 List
ReadOnlyCollection<IWebElement>
.因此,您可以像这样简化代码:
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);
}
这篇关于C#如何单击IList中的IWebelement?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文