java - How to Click Random element from a list of elements returned by a search -
i have page list of products displayed, need click particular type of listings list saved particular type "webelement". now, whenever land on page, check condition , click on first product. but, requirement after checking condition, need click random product in list. see code below. kindly suggest.
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']")); (int i=0; < 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 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();
you can use random generator class.
random rand = new random(); string locator = string.format("//div/div[2]/div[{0}]/input", rand.next(5)); // 5 number of elements in list driver.findelement(by.xpath(locator)).click();
this code in c# 1 1 in java. also, change xpath locator if not correct locator of list.
Comments
Post a Comment