简体   繁体   English

有没有办法等待 SendKeys() 完成?

[英]Is there a way to wait for SendKeys() to finish?

Currently, in our framework, we have a function that does a SendKeys.目前,在我们的框架中,我们有一个执行 SendKeys 的函数。 Before of actually triggering the SendKeys we request for the value, and after the sendkeys we check if that value has changed, to trigger a javascript function to set the value of the field.在实际触发 SendKeys 之前,我们请求值,在 sendkeys 之后,我们检查该值是否已更改,以触发 javascript 函数来设置字段的值。 This is in case the SendKeys method didn't work (we've had an specific case).这是在 SendKeys 方法不起作用的情况下(我们有一个特定的案例)。

Thing is, it is all happening so fast that when it request for the field value after the SendKeys method, it hasn't changed, so it triggers the javascript function anyway.事情是,这一切发生得如此之快,以至于当它在 SendKeys 方法之后请求字段值时,它并没有改变,所以它无论如何都会触发 javascript 函数。

Is there any way to avoid this, that doesn't involve putting a Thread.Sleep() in it?有没有办法避免这种情况,不涉及在其中放置 Thread.Sleep() ?

Alternatively, you can try Actions class to send the text, Refer below java snippet:或者,您可以尝试使用Actions类发送文本,请参阅下面的 java 代码段:

public static void enterTextboxValue(WebDriver driver, WebElement element, String value) {

    Actions navigator = new Actions(driver);
    navigator.click(element).sendKeys(Keys.END).keyDown(Keys.SHIFT).sendKeys(Keys.HOME).keyUp(Keys.SHIFT)
            .sendKeys(Keys.BACK_SPACE).sendKeys(value).perform();

    if (element.getAttribute("value").contains(value)) {
        System.out.println("Value [ " + value + " ] entered");
    } else {
        System.out.println("Unable to enter the value");
    }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM