I'm trying to making automation and I need to select random product in page.
That's my code and it's not working.
//Select random product.
List<WebElement> TumUrunler= driver.findElements(By.xpath("//div[@class='m-grid-col-9']"));
int TumListe= TumUrunler.size();
Random random= new Random();
int RandomUrun= random.nextInt(TumListe);
TumUrunler.get(RandomUrun).click();
I'm trying to select random product in this link https://www.turkcell.com.tr/pasaj/cep-telefonu
CodePudding user response:
You are using a wrong locator. //div[@class='m-grid-col-9']
is matching some container where all those products inside it. You can use this XPath instead:
"//div[@class='m-grid-col-4 product']"
The locator above can even be shortened to this CSS Selector:
"div.product"
So, instead of List<WebElement> TumUrunler= driver.findElements(By.xpath("//div[@class='m-grid-col-9']"));
please try
List<WebElement> TumUrunler= driver.findElements(By.cssSelector("div.product"));
Also, you will need to scroll the randomly selected element into the view since not all the elements are initially inside the visible view port of the screen.