【问题标题】:Selenium Webdriver (Java) - How to work around dynamic text and countdown timer?Selenium Webdriver (Java) - 如何解决动态文本和倒数计时器?
【发布时间】:2019-06-21 14:14:10
【问题描述】:

我正在致力于自动化包含计数器的注册。

所以问题是,我想报名参加每 7 分钟发生一次的活动。

这是网页代码的样子:

<class-countdown-timer _ngcontent-c19="" _nghost-c24="">
  <!---->
  <h2 _ngcontent-c24="" id="class-countdown-timer">Próxima clase en vivo en <span _ngcontent-c24="" class="countdown-timer">02:15</span>
    <i _ngcontent-c24="" aria-hidden="true" class="icon-clock"></i>
  </h2>
</class-countdown-timer>

我的问题是“class-countdown-timer”文本是动态的,“countdown-timer”是倒计时,从 07:00 到 00:00。

当“class-countdown-timer”文本为“Próxima clase en vivo en”并且计数器在“05:00”和“02:00”之间时,我的求助是我需要执行某个操作"

我无法流畅地等待,直到第一个,倒计时文本显示以上内容并且计时器在这些时间之间,知道吗?

谢谢 :D

【问题讨论】:

  • 嗨杰夫!我是 Stack Overflow 的新用户,也是编程场景的新手!感谢您的评论,我会这样做以解决更多问题!
  • 该建议不仅适用于未来的问题,也适用于这个问题。 每个问题都遵循指南和标准,这一点很重要。

标签: java selenium selenium-webdriver webdriver wait


【解决方案1】:

试试下面的代码:

// Count-down timer for looping upto 8 minutes
        int countdown = 480;
        while(countdown-->0) {
            // Fetching the actual class-countdown-timer value
            String actualTimerText = driver.findElement(By.id("class-countdown-timer")).getText().trim();
            String expectedTimerText = "Próxima clase en vivo en ";

            // Fetching the actual countdown-timer value
            String countdownTimer = driver.findElement(By.xpath("//span[@class='countdown-timer']")).getText().trim();

            // Converting the time for better comparing
            SimpleDateFormat format = new SimpleDateFormat("hh:mm");
            Date expectedFirstTime = format.parse("05:00"), expectedSecondTime = format.parse("02:00");
            Date actualCountdownTimer = format.parse(countdownTimer);

            // Checking the condition if the 'class-countdown-timer' text is 'Próxima clase en vivo ee' and counter is in between '05:00' and '02:00' or not? 
            if(actualTimerText.equals(expectedTimerText) && ((actualCountdownTimer.equals(expectedSecondTime) || actualCountdownTimer.after(expectedSecondTime)) && (actualCountdownTimer.equals(expectedFirstTime) || actualCountdownTimer.before(expectedFirstTime)))) {
                System.out.println("Condition Satisfied...");
                // Do something
                break;
            }
            // Waiting for one second before finding the element and checking the condition
            Thread.sleep(1000);
        }

上述方法是使用循环的概念,最多迭代 8 分钟(480 秒),直到满足所需条件。

【讨论】:

  • 嗨阿里!哇!感谢您的快速响应和帮助!我会用一些问题来打扰您,以充分了解您的反馈! :D 第一段代码:将倒计时初始化为 480,但不明白当倒计时大于 0 时的 while 条件“countdown-->0”是什么?由于倒数计时器和文字各不相同,从“Proxima clase”到“Proxima registracion”,两者都有 7 分钟倒计时。当“actualTimerText.equals(expectedTimerText)”不匹配时会发生什么?
  • 从第二段代码开始:第一部分“class-countdown-timer”始终可用于不同的文本。 “//Span ...倒数计时器”也是如此。我不明白第一个流利的等待有什么作用或为什么要使用它们。很多很多很多谢谢
  • 在第一段代码中,最多 8 分钟(480 秒)我们正在尝试使所需条件发生(“class-countdown-timer”文本是“Proxima case en vivo en”并且计数器在“05:00”和“02:00”之间)并且我正在减少“倒计时”变量值直到它变为负数,因为如果我们在 8 分钟内找不到答案,那么 countdown--&gt;0 将打破 while 循环。
  • 你在第二段代码中抓住了好点,需要修改一下
  • actualTimerText.equals(expectedTimerText) 不匹配时,其他条件将被执行actualTimerText.greater(expectedTimerText) => 这就像单独检查sometime &gt;= 02:00 和'sometime
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多