所以我尝试单击 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();
根据文档Selenium https://seleniumhq.github.io/selenium/docs/api/dotnet/html/M_OpenQA_Selenium_ISearchContext_FindElements.htm 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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)