0

I am currently doing a small project on web automation. Its for online betting at live roulette lounges,

my problem is this, since these are live-streamed events they have controls in place that show you things on screen and also control when and when not a bet can be placed on a number.

one of these controls is a 15 second clock that appears and disappears, while visible and counting down you can place bets, when it runs out it disappears and you have to wait for the dealer to spin the ball and for the result and clock to reappear before you can bet again.

I am looking to automate the whole process of bets being placed and certain things happening under certain conditions.

But is based around when that clock is visible because that is the only time you can bet, and since there is no definite time that the ball will have spun and landed, my only real option is to wait for the clock element to be visible (the html it appears and re-appears in the inspector on chrome ( i presume this is ajax being used ) )

so i am looking to use a fluent wait with no timeout ( it will quite literally just wait for the element to appear because it will appear no matter what)

is there any way to make a "wait" in selenium ( with java ) so that there is no timeout but you can poll every second ??? for example i know sometimes 0 is used to say no time limit... can anyone help ?

i have an example of code here this is the only part i need help with, i know it has to be simple and no long outrageous code is required.

cheers guys

WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("my_element")));
Andrei Fiordean
  • 225
  • 3
  • 14

3 Answers3

0

As you are trying to invoke click() on the element so, instead of using presenceOfElementLocated() you need use elementToBeClickable() and you can use either of the following Locator Strategies:

  • Using cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("cssSelector_my_element"))).click();
    
  • Using xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("xpath_my_element"))).click();
    

In the above code solutions, polling is set to default i.e. 500 ms. You can set the polling to 1 sec as follows:

  • Using cssSelector:

    new WebDriverWait(driver, 20, 1).until(ExpectedConditions.elementToBeClickable(By.cssSelector("cssSelector_my_element"))).click();
    
  • Using xpath:

    new WebDriverWait(driver, 20, 1).until(ExpectedConditions.elementToBeClickable(By.xpath("xpath_my_element"))).click();
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
0

Yes, There is a way for polling. But you have to set timeOut. This will give you the solution. In this initialization 3rd parameter is polling time. In every 1 second it will do polling for that element.

WebElement myDynamicElement = (new WebDriverWait(driver, 60 , 1))
.until(ExpectedConditions.presenceOfElementLocated(By.id("my_element")));
sp324
  • 275
  • 1
  • 20
  • thank you but what i wanted to know was can you put an unlimited timeout on the wait? so it will wait as long as it takes until the Element is visible again ?? – Dean Preston Apr 02 '19 at 13:31
  • @DeanPreston There is no way to have infinite timeout . And also It's a not a good practice to have a infinite timeout. – sp324 Apr 03 '19 at 07:26
0

I don's see how it's possible. This is how the until method is implemented:

    def until(self, method, message=''):
    """Calls the method provided with the driver as an argument until the \
    return value is not False."""
    screen = None
    stacktrace = None

    end_time = time.time() + self._timeout
    while True:
        try:
            value = method(self._driver)
            if value:
                return value
        except self._ignored_exceptions as exc:
            screen = getattr(exc, 'screen', None)
            stacktrace = getattr(exc, 'stacktrace', None)
        time.sleep(self._poll)
        if time.time() > end_time:
            break
    raise TimeoutException(message, screen, stacktrace)

As you can see current time is compared with the end_time which is the time when the function is called plus the timeout. To solve your problem I would use some pretty big number of seconds.