需要等待超时的帮助

问题描述 投票:0回答:3

我目前正在做一个关于网络自动化的小项目。它用于现场轮盘赌场的在线投注,

我的问题是这个,因为这些是实时流式事件,他们有控件到位,在屏幕上显示事物,并控制何时何时不能下注数字。

其中一个控制是一个15秒的时钟出现和消失,而可见和倒计时,你可以下注,当它用完它消失,你必须等待经销商旋转球,结果和时钟再次出现在你再次下注之前。

我希望自动完成投注的整个过程以及在某些条件下发生的某些事情。

但是基于那个时钟可见时,因为这是你唯一可以下注的时间,而且由于没有确定的时间球将旋转和着陆,我唯一真正的选择是wait for the clock element可见(它出现的html并重新出现在检查员的铬(我认为这是使用ajax))

所以我希望使用一个流畅的等待,没有超时(它将完全等待元素出现,因为它会出现无论什么)

有没有办法在selenium(使用java)中进行“等待”,这样就没有超时但你可以每秒轮询???例如我知道有时候0用来说没有时间限制......任何人都可以帮忙吗?

我有一个代码示例,这是我需要帮助的唯一部分,我知道它必须简单,不需要冗长的代码。

欢呼的家伙

WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("my_element")));
java selenium wait webdriverwait fluentwait
3个回答
0
投票

是的,有一种方法可以进行投票。但你必须设置timeOut。这将为您提供解决方案。在此初始化中,第3个参数是轮询时间。在每1秒钟内,它将对该元素进行轮询。

WebElement myDynamicElement = (new WebDriverWait(driver, 60 , 1))
.until(ExpectedConditions.presenceOfElementLocated(By.id("my_element")));

0
投票

当您尝试在元素上调用click()时,您需要使用presenceOfElementLocated()而不是使用elementToBeClickable(),您可以使用以下任一Locator Strategies

  • 使用cssSelectornew WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("cssSelector_my_element"))).click();
  • 使用xpathnew WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("xpath_my_element"))).click();

在上面的代码解决方案中,轮询被设置为default,即500 ms。您可以将轮询设置为1 sec,如下所示:

  • 使用cssSelectornew WebDriverWait(driver, 20, 1).until(ExpectedConditions.elementToBeClickable(By.cssSelector("cssSelector_my_element"))).click();
  • 使用xpathnew WebDriverWait(driver, 20, 1).until(ExpectedConditions.elementToBeClickable(By.xpath("xpath_my_element"))).click();

0
投票

我不明白它是怎么可能的。这就是until方法的实现方式:

    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)

如您所见,当前时间与end_time进行比较,qazxswpoi是调用函数的时间加上超时。为了解决你的问题,我会使用相当多的秒数。

© www.soinside.com 2019 - 2024. All rights reserved.