简体   繁体   English

Selenium c# 随机点击到ClassName

[英]Selenium c# random click to the ClassName

I am trying to do random clicks to the ClassName in Selenium c#.我正在尝试对 Selenium c# 中的 ClassName 进行随机点击。 This is my code, please, help to understand what is wrong here.这是我的代码,请帮助理解这里有什么问题。 It doesn't work.它不起作用。

List<IWebElement> elements = new List<IWebElement>(_driver.FindElements(By.ClassName("mvp-vid-box-wrap mvp-vid-marg")));
Random random = new Random();
int num = random.Next(0, elements.Count);
elements[num].Click();
Thanks in advance.
 //I did this in main so I needed to create a new object to call my driver
 Program p = new Program();

 p.driver.Url = "https://www.javatpoint.com/selenium-webdriver-locating-strategies-by-class-name";

 //IWebElement list = p.driver.FindElement(By.ClassName("ddsmoothmenu"));

 //Here I use XPath instead of class to better retrieve the contents of the list im accessing
 List<IWebElement> elements = new List<IWebElement>(p.driver.FindElements(By.XPath("//*[@class='ddsmoothmenu']/ul/li/a")));

 Random random = new Random();
 int num = random.Next(0, elements.Count);
 elements[num].Click();

 //Writes the amount of elements in your element list to the console.
 Console.WriteLine("There are " + elements.Count +" elements in this list");

I ended up having to use XPath for this to work before I did the element list only had one object in it so there was ever only one item the program was actually adding to the list.我最终不得不使用 XPath 来让它工作,然后我才做元素列表只有一个 object ,所以程序实际上只有一个项目添加到列表中。 After using XPath it was populated by 20.使用 XPath 后,它被填充了 20。

Side note, there is an issue starting at index 14 and beyond for me where the item in the index is "Unhandled Exception: OpenQA.Selenium.ElementNotInteractableException: element not interactable".旁注,对我来说,从索引 14 及以后开始存在一个问题,其中索引中的项目是“未处理的异常:OpenQA.Selenium.ElementNotInteractableException:元素不可交互”。 So you may want to make sure that not only is your XPath correct but that all the actual information it grabs is useable.因此,您可能需要确保不仅您的 XPath 正确,而且它获取的所有实际信息都是可用的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM