所以我尝试单击 YouTube 上的按钮,但我无法通过 Xpath 找到该按钮,因为按钮太多,所以我尝试将它们保存在 IList 中,现在我想单击 YouTube 中的特定按钮列表。
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();
最佳答案
根据 Selenium
的文档<强> FindElements(By.XPath("//*[@id=\"button"]"))
将返回 List
类型 ReadOnlyCollection<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']"));
foreach (IWebElement field in textfields)
{
string my_text = field.GetAttribute("any_attribute_button_tag");
Console.WriteLine(my_text);
}
关于C# 如何单击 IList 中的 IWebelement?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47485022/