Module 17 Exercise ---- Random Click | Selenium Forum
M
Posted on 03/07/2016
Hi,
On Module 17 Exercise Point No . 6 ------->
I am facing below issue during click on Random click of any five products and they should add into cart. I think my logic is wrong so please tell me 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;

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();
}

//WebElement allAddtoCarts = driver.findElement(By.xpath("//*[starts-with(@id,'p_')]"));
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();
//carts.get(total).click();
//Thread.sleep(3000L);
carts.get(i).click();

}
}
}


ERROR :-

Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Command duration or timeout: 10.11 seconds
Build info: version: '2.51.0', revision: '1af067d', time: '2016-02-05 19:11:55'
System info: host: 'user-PC', ip: '192.168.1.70', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=46.0.1, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: d3b22535-5eae-4753-b896-b11f9ada7769
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:327)
at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:85)
at Sam_BigBasket_RandomClick.main(Sam_BigBasket_RandomClick.java:37)
Caused by: org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Build info: version: '2.51.0', revision: '1af067d', time: '2016-02-05 19:11:55'
System info: host: 'user-PC', ip: '192.168.1.70', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0'
Driver info: driver.version: unknown
at <anonymous class>.fxdriver.preconditions.visible(file:///C:/Users/user/AppData/Local/Temp/anonymous4850392777036789839webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:10045)
at <anonymous class>.DelayedCommand.prototype.checkPreconditions_(file:///C:/Users/user/AppData/Local/Temp/anonymous4850392777036789839webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12585)
at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/user/AppData/Local/Temp/anonymous4850392777036789839webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12602)
at <anonymous class>.fxdriver.Timer.prototype.setTimeout/<.notify(file:///C:/Users/user/AppData/Local/Temp/anonymous4850392777036789839webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:623)

M
Replied on 03/07/2016

Any Update????


M
Replied on 03/07/2016

use this

[quote:3fki5wep]Random r = new Random();
carts.get(r.nextInt(total)).click();
[/quote:3fki5wep]


M
Replied on 04/07/2016

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_')]")));

}
}}

Responsive image

M
Replied on 05/07/2016

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.


M
Replied on 05/07/2016

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