java 如何从搜索返回的元素列表中单击随机元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30189450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Click Random element from a list of elements returned by a search
提问by Aditya
I have page where list of products are displayed, I need to click only particular type of listings from the list so saved this particular type as "Webelement". Now, whenever I land on this page, I check a condition and click only on first product. But, my requirement is after checking a condition, I need to click any random product in the list. see my code below. kindly suggest.
我有显示产品列表的页面,我只需要从列表中单击特定类型的列表,以便将此特定类型保存为“Webelement”。现在,每当我登陆这个页面时,我都会检查一个条件并只点击第一个产品。但是,我的要求是在检查条件后,我需要单击列表中的任何随机产品。请参阅下面的代码。请建议。
driver.findElement(By.linkText("ALL EQUIPMENT")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
List <WebElement> listings = driver.findElements(By.cssSelector("a[href*='/listing?listingid']"));
for (int i=0; i < listings.size(); i++) {
WebElement requiredlisting = listings.get(i);
System.out.println(i);
requiredlisting.click();
Thread.sleep(10000);
getvalue = driver.findElement(By.xpath("//div[7]/span")).getText();
System.out.println(getvalue);
driver.findElement(By.xpath("//div[3]/div[2]/input")).click();
Thread.sleep(10000);
driver.findElement(By.id("listingQuestion")).click();
Thread.sleep(10000);
driver.findElement(By.id("listingQuestion")).sendKeys("Where is the listing located");
Thread.sleep(10000);
driver.findElement(By.name("submitq")).click();
Thread.sleep(10000);
driver.findElement(By.xpath("//div/div[2]/div[3]/input")).click();
Thread.sleep(10000);
driver.findElement(By.id("uname")).click();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.linkText("Sign Out")).click();
回答by Anton Angelov
You can use the Random generator class.
您可以使用随机生成器类。
Random rand = new Random();
string locator = string.Format("//div/div[2]/div[{0}]/input", rand.Next(5));
// Where 5 is the number of the elements in the list
driver.FindElement(By.Xpath(locator)).Click();
This is the code in C# it is almost 1 to 1 as in Java. Also, change the Xpath locator if this is not the correct locator of your list.
这是 C# 中的代码,与 Java 中的代码几乎是 1 对 1 的。此外,如果这不是您列表中的正确定位器,请更改 Xpath 定位器。
回答by Subh
So, since your requirement is to click on any random element, then don't use for-loop for clicking on the element. You can use the Randomclass and initialize its object with the size of the list and click on any element on the list as below:
因此,由于您的要求是单击任何随机元素,因此不要使用 for-loop 来单击该元素。您可以使用Random类并使用列表的大小初始化其对象,然后单击列表中的任何元素,如下所示:
List <WebElement> listings = driver.findElements(By.cssSelector("a[href*='/listing?listingid']"));
Random r = new Random();
int randomValue = r.nextInt(listings.size()); //Getting a random value that is between 0 and (list's size)-1
listings.get(randomValue).click(); //Clicking on the random item in the list.