在等待超时方面需要帮助

时间:2019-04-02 12:23:21

标签: java selenium wait webdriverwait fluentwait

我目前正在做一个有关Web自动化的小项目。用于在现场轮盘休息室进行在线投注,

我的问题是,由于这些是直播事件,因此它们具有控件,可以在屏幕上向您显示内容,还可以控制何时以及何时不能对数字下注。

其中一个控制项是一个15秒的时钟,它出现和消失,而可见并倒计时,您可以下注,当它用完时,它消失,您必须等待发牌手旋转球并等待结果和时钟重新出现,然后才能再次下注。

我希望使下注和在某些情况下发生的某些事情的整个过程自动化。

但是基于该时钟何时可见,因为这是您唯一可以下注的时间,并且由于没有确定的时间球会旋转并着陆,所以我唯一的选择是wait for the clock element可见(html会出现并重新出现在chrome上的检查器中(我假设这是ajax被使用))

所以我希望使用没有超时的流畅等待(它实际上只是等待元素出现,因为无论如何都会出现)

有什么方法可以在硒中(使用java)进行“等待”,以便没有超时,但是您可以每秒轮询一次?例如我知道有时候0用来表示没有时间限制...任何人都可以帮忙吗?

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

欢呼声

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

3 个答案:

答案 0 :(得分:0)

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

  • 使用cssSelector

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

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

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

  • 使用cssSelector

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

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

答案 1 :(得分:0)

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

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

答案 2 :(得分: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进行比较,function mountRecipient(user_email){ window.parent.console.log(user_email); } 是函数被调用的时间加上超时时间。 为了解决您的问题,我将花费大量的时间。

相关问题