简体   繁体   English

Java AWT Robot + Selenium:如何在发送关键事件后重新获得Web元素的控制权

[英]Java AWT Robot + Selenium: How to get back hold of the web elements after sending key events

I have written the following code to handle a native OS pop up that is raised after clicking on a verification link in a webpage. 我编写了以下代码来处理在单击网页中的验证链接后引发的本机OS弹出窗口。 It almost works, except that after the key presses are sent away to the pop up and next page is loaded I don't have control over my driver anymore. 几乎可以正常工作,除了在将按键发送到弹出窗口并加载下一页后,我不再可以控制驱动程序了。 No Selenium code I have tried after, seems to be able to work properly. 我尝试过的Selenium代码似乎无法正常工作。 Either a WebDriverException is raised (from some internal Selenium Javascript file) or a time out Exception is raised. (从某些内部的Selenium Javascript文件中)引发WebDriverException或引发超时Exception。 I've experimented with getting the page handle and trying to switch back to it, explicitly wait with FluentWait until an element in the new page is present and have tried different Selenium code after with no avail. 我已经尝试过获取页面句柄并尝试切换回它,显式地等待FluentWait直到出现新页面中的元素,并尝试了不同的Selenium代码,但无济于事。 Any cues to understand the problem here is much appreciated. 任何了解此处问题的线索都将受到赞赏。

Here is my Java code snippet with some inline comments: 这是我的Java代码片段,其中包含一些内联注释:

    driver = new FirefoxDriver();
    // String windowHandle = driver.getWindowHandle(); //Saving the window handle to switch to it later
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.navigate().to("https://mysite/login"); //Website with native OS pop up for authentication

    driver.findElement(By.cssSelector(".loginbox")).click();

    //Convenience method to send a series of key codes to java.awt.Robot
    sendKeySequence(KeyEvent.VK_TAB, KeyEvent.VK_TAB, KeyEvent.VK_ENTER);

    //TRYING TO GET HOLD OF THE WEB ELEMENTS AGAIN: Nothing works!

    //1st try: fluentWait(By.id("BigBox")).click(); //Method to wait until WebDriver element is present on the page

    //2nd try: driver.switchTo().window(windowHandle);
    //driver.get("www.google.com");

sendKeySequence Implementation: sendKeySequence实现:

  private void sendKeySequence (int... commandStream) {
    Robot robot = new Robot();
    for (int keyCode : commandStream) {
      robot.keyPress(keyCode);
      robot.keyRelease(keyCode);
    }
  }

PS! PS! For the fluentWait implementation refer to this post 对于fluentWait实现,请参阅这篇文章

Try using the robot like this ( Assuming* you need to press 2 TAB keys and 1 Enter Key using Robot class ): 尝试像这样使用机器人( 假设*您需要按下2个TAB键和1个使用Robot类的Enter键 ):

    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_TAB);
    robot.keyRelease(KeyEvent.VK_TAB);
    robot.keyPress(KeyEvent.VK_TAB);
    robot.keyRelease(KeyEvent.VK_TAB);
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);

Then, try waiting for the new element in the page, using the below code: 然后,尝试使用以下代码等待页面中的新元素:

    try{
        WebDriverWait wait = new WebDriverWait(driver,30);
        WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//xpath of the element")));
        element.click();
    }catch(Throwable e){
        System.err.println("Error came while waiting and clicking the element. "+e.getMessage());
    }

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

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