1

I have opened a search dialog box with the help of robot class but now I am trying to enter the text in that search dialog box but it is not entering the text with help of robot keypress.

The source code is as below

On Click of Add Row button as below a search dialog window opened up

driver.findElement(By.xpath("//*[@id=\"sale_dtl_ig_toolbar\"]/div[1]/div[6]/button")).click();

// Code to open search dialog box

    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_ALT);
    robot.keyPress(KeyEvent.VK_S);

    robot.keyRelease(KeyEvent.VK_ALT);
    robot.keyRelease(KeyEvent.VK_S);
    Thread.sleep(5000); // sometimes there may be a delay for the dialog box to get appeared
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);

    Dimension i = driver.manage().window().getSize();
    System.out.println("Dimension x and y  :"+i.getHeight()+","+i.getWidth());

    int x = (i.getWidth()/4 )+ 20;
    int y = (i.getHeight()/10)+ 50;

    robot.mouseMove(x,y);

    robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
    robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
    ....................... upto this code search dialog window appears with one search dropdown

    //driver.findElement(By.id("SEARCH")).sendKeys("");
  //*[@id="SEARCH"] ............ here i had tried to locate the element but it is thowing exception unable to locate element

Then i used below code to type in the search field but it also doesn;t work

    robot.keyPress(KeyEvent.VK_L);
    robot.keyRelease(KeyEvent.VK_L);
    robot.keyPress(KeyEvent.VK_A);
    robot.keyRelease(KeyEvent.VK_A);
    robot.keyPress(KeyEvent.VK_B);
    robot.keyRelease(KeyEvent.VK_B);
    robot.keyPress(KeyEvent.VK_O);
    robot.keyRelease(KeyEvent.VK_O);
    robot.keyPress(KeyEvent.VK_U);
    robot.keyRelease(KeyEvent.VK_U);
    robot.keyPress(KeyEvent.VK_R);
    robot.keyRelease(KeyEvent.VK_R);
    robot.keyPress(KeyEvent.VK_SPACE);
    robot.keyPress(KeyEvent.VK_C);
    robot.keyRelease(KeyEvent.VK_C);
    robot.keyPress(KeyEvent.VK_H);
    robot.keyRelease(KeyEvent.VK_H);
    robot.keyPress(KeyEvent.VK_A);
    robot.keyRelease(KeyEvent.VK_A);
    robot.keyPress(KeyEvent.VK_R);
    robot.keyRelease(KeyEvent.VK_R);
    robot.keyPress(KeyEvent.VK_G);
    robot.keyRelease(KeyEvent.VK_G);
    robot.keyPress(KeyEvent.VK_E);
    robot.keyRelease(KeyEvent.VK_E);
    robot.keyPress(KeyEvent.VK_S);
    robot.keyRelease(KeyEvent.VK_S);
2
  • 1
    Are you not able to send the text through Selenium sendKeys() command? Also, can you give the error if any and the page HTML structure that would be helpful Commented May 5, 2020 at 0:05
  • I don't understand why you are using Java Robot instead of using Selenium's browser interaction methods like Actions. selenium.dev/selenium/docs/api/java/org/openqa/selenium/… Commented Jun 6, 2020 at 1:18

1 Answer 1

1

Maybe the DOM hasn't created yet. Use WebDriverWait to make sure that the DOM be created before sendKeys:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "SEARCH"))

element.sendKeys(" ")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.