1

I would like to click multiple times in the HTML ID.

I have this code, that just click 1 time per the time the user defines:

What I mean is that the user writes in the input the timeout and that in that user defined timeout, the "code" makes maximum clicks in that timeframe.

What i need is a Loop of clicks

UPDATE:

RESOLVED

3 Answers3

1

Here:

element.click();

clicks once.

element.click();
element.click();

clicks twice.

for (int i=0; i < 10; i ++) {
  element.click();
}

clicks 10 times.

If you want to run things for a specific time, see here for example.

The point is: then you need to do:

  • fetch a timestamp
  • add say N seconds to that timestamp
  • keep looping until "now" becomes that target timestamp

Of course, in 2019, you want to use the Java8 time/date functions, like: here

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Its like the WHILE LOOP – Marcelo Domingues Nov 20 '19 at 15:25
  • @MarceloDomingues I updated the answer again. But please understand: this is really basic stuff, and well, you researching "how to use a while loop" is really something you could / should do on your own ... – GhostCat Nov 20 '19 at 15:39
  • the questioner wants to do it concurrently is what i believe. not a while loop or for loop. which i believe is already known. – Srini M Nov 25 '19 at 10:24
  • @SriniM Maybe, maybe not. The OP was asked repeatedly to clarify, nothing happened. It is hard to say what he really wants. I tried to answer as best as I could, given his input. – GhostCat Nov 25 '19 at 10:26
1

You are then coming to a concurrent click scenario. Use java executor service and assign the click job as a task. Then execute all on the executor service. This will make the click on the element concurrently. Far faster than for loops etc.

Refer here for a solution like below. The tasks [task 1, task 2 and task 3] are your click events from Selenium. Ideally for you its the same job. So run a forloop and add same callable to the list.

ExecutorService executorService = Executors.newSingleThreadExecutor();

Set<Callable<String>> callables = new HashSet<Callable<String>>();

callables.add(new Callable<String>() {
public String call() throws Exception {
    return "Task 1";
}
});
callables.add(new Callable<String>() {
public String call() throws Exception {
    return "Task 2";
}
});
callables.add(new Callable<String>() {
public String call() throws Exception {
    return "Task 3";
}
});

List<Future<String>> futures = executorService.invokeAll(callables);

for(Future<String> future : futures){
System.out.println("future.get = " + future.get());
}

executorService.shutdown();
Srini M
  • 495
  • 2
  • 9
1

You can import the following dependency to use the StopWatch object:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.7</version>
</dependency>

And the code:

long userDefinedInMillis = 10000; //define a time range in millis
int noOfClicks = 0;

// we use the StopWatch object here to keep track of time
StopWatch watch = new StopWatch();
watch.start();
while(watch.getTime() < userDefinedInMillis){
    element.click();
    noOfClicks++;
}
watch.stop();

System.out.println("Total no. of clicks: " + noOfClicks);

Note that this class is not a thread-safe.

https://commons.apache.org/proper/commons-lang/javadocs/api-3.9/org/apache/commons/lang3/time/StopWatch.html#getTime--

Timothy T.
  • 1,031
  • 1
  • 12
  • 25