use this
[quote:3fki5wep]Random r = new Random();
carts.get(r.nextInt(total)).click();
[/quote:3fki5wep]
Hi,
I modify my code with [b:19cb0xqx]Explicitly Wait[/b:19cb0xqx]. So some time my code add one product into cart and some time not. Also not able to add 5 random products
into cart so please look into below code and suggest where i am wrong.
Code :-
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Sam_BigBasket_RandomClick {
static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://bigbasket.com/");
driver.findElement(By.xpath("//*[@id='skip_explore']")).click();
for(int i=1;i<=1;i++){
driver.findElement(By.xpath("//*[@id='basket_menu']/ul/li['"+i+"']/a")).click();
}
List<WebElement> carts = driver.findElements(By.xpath("//*[starts-with(@id,'p_')]"));
for(int i =1;i<=5;i++){
int total = carts.size();
Random r = new Random();
carts.get(r.nextInt(total)).click();
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[starts-with(@id,'p_')]")));
}
}}
it is heavy website it take a long time to load. so, it is giving me an [color=#FF0000:j1k2zd0h] org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Command duration or timeout: 10.07 seconds[/color:j1k2zd0h]
your code seems to be fine.
in your carts there are lot of empty spaces remove them using this.
http://stackoverflow.com/questions/5520693/in-java-remove-empty-elements-from-a-list-of-strings