简体   繁体   中英

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. 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. Either a WebDriverException is raised (from some internal Selenium Javascript file) or a time out Exception is raised. 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. Any cues to understand the problem here is much appreciated.

Here is my Java code snippet with some inline comments:

    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:

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

PS! For the fluentWait implementation refer to this post

Try using the robot like this ( Assuming* you need to press 2 TAB keys and 1 Enter Key using Robot class ):

    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());
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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