【问题标题】:How to automate an analog clock with selenium webdriver and Java?如何使用 selenium webdriver 和 Java 自动化模拟时钟?
【发布时间】:2020-07-07 04:33:32
【问题描述】:

上下文:我必须使用以下规则自动设置时间:配置的时间必须比当前时间长 40 分钟。

字段详情:该字段不可编辑。当我点击该字段时,我按下一个模拟时钟弹出窗口,在我手动选择时间后,该字段开始显示所选时间(规则:我必须选择比当前时间多 40 分钟)

问题:我如何让我的自动化获取当前时间、添加 40 分钟并将此值放入字段中而无需打开弹出窗口?

我试过了,但没用: driver.findElement(By.name("dateSend")).sendKeys("22/07/2020-17:28");

错误:org.openqa.selenium.ElementNotInteractableException:元素不可交互

下面是代码和字段的图片:

Imagem do campo: não editável enter image description here

Código do campo enter image description here

Imagem 弹出窗口 enter image description here

Código 弹出窗口 enter image description here

Campo 'Disparo' após selecioada a hora: enter image description here

Código do campo com horário enter image description here

【问题讨论】:

    标签: java selenium-webdriver automation popup


    【解决方案1】:

    ElementNotInteractableException

    它表示即使元素存在于 DOM 中,但目前它还没有处于交互状态。详细解释参考thisSO线程。

    可能的解决方案

    现在来回答你的问题。使用以下代码将 40 分钟添加到当前时间:

    String updateTime = "";
    DateFormat timeFormat = new SimpleDateFormat("HH:mm");
    Calendar cal = Calendar.getInstance();
    //Adding 40 minute in current time
    cal.add(Calendar.MINUTE, 40)
    
    updateTime = timeFormat.format(cal.getTime());
    

    然后您可以尝试将元素中的值设置为:

    driver.findElement(By.name("dateSend")).sendKeys("value", updateTime);
    

    或使用 JavaScript

    WebElement element = driver.findElement(By.name("dateSend"));
     JavascriptExecutor jse = (JavascriptExecutor)driver;
     jse.executeScript("arguments[0].value='"+updateTime+"';", element);
    

    另外,从附图看,模拟时钟有 12 小时模式。尝试在 AM 和 PM 模式下都设置值,然后据此设置SimpleDateFormat

    【讨论】:

      猜你喜欢
      • 2023-03-03
      • 1970-01-01
      • 2012-03-15
      • 1970-01-01
      • 1970-01-01
      • 2013-04-14
      • 1970-01-01
      • 1970-01-01
      • 2016-12-06
      相关资源
      最近更新 更多