Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/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
Selenium c#随机单击类名_C#_Selenium - Fatal编程技术网

Selenium c#随机单击类名

Selenium c#随机单击类名,c#,selenium,C#,Selenium,我试图在Selenium c#中随机单击类名。 这是我的代码,请帮助理解这里的错误。它不起作用 List elements=新列表(_driver.FindElements(By.ClassName(“mvp vid box wrap mvp vid marg”)); 随机=新随机(); int num=random.Next(0,elements.Count); 元素[num]。单击(); 提前谢谢。 //我在main中这样做了,所以我需要创建一个新对象来调用我的驱动程序 程序p=新程序();

我试图在Selenium c#中随机单击类名。 这是我的代码,请帮助理解这里的错误。它不起作用

List elements=新列表(_driver.FindElements(By.ClassName(“mvp vid box wrap mvp vid marg”));
随机=新随机();
int num=random.Next(0,elements.Count);
元素[num]。单击();
提前谢谢。
//我在main中这样做了,所以我需要创建一个新对象来调用我的驱动程序
程序p=新程序();
p、 驱动程序Url=”https://www.javatpoint.com/selenium-webdriver-locating-strategies-by-class-name";
//IWebElement list=p.driver.FindElement(By.ClassName(“ddsmoothmenu”);
//这里我使用XPath而不是类来更好地检索im访问的列表的内容
列表元素=新列表(p.driver.FindElements(By.XPath(“//*[@class='ddsmoothmens']]/ul/li/a”);
随机=新随机();
int num=random.Next(0,elements.Count);
元素[num]。单击();
//将元素列表中的元素数量写入控制台。
Console.WriteLine(“此列表中有“+元素.计数+”元素”);
在我做元素列表之前,我不得不使用XPath来实现这一点。元素列表中只有一个对象,因此程序实际上只向列表中添加了一项。使用XPath后,它填充了20个


旁注,对于我来说,从索引14及其后开始有一个问题,索引中的项是“未处理的异常:OpenQA.Selenium.elementnotinteractiableexception:element notinteractiableexception”。因此,您可能需要确保XPath不仅正确,而且它所获取的所有实际信息都是可用的。

元素和
class_元素
不是一回事…@Jonathan,是的,这是我的错误。thanks@Jonathan我已经更改了它,但是当您打印元素时,它仍然不起作用。计数,您得到了什么值?
 //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");