Java Selenium在将文本输入文本字段后使用鼠标来选择特定文本?

时间:2018-11-09 09:36:23

标签: java selenium robot

在将文本输入到文本字段后,是否可以使用Java硒通过鼠标创建对特定文本的选择? (如下图所示)

我尝试使用硒的Actions或Robot来进行选择,但是它们似乎仅支持通过键盘进行文本选择,而对于必须仅通过鼠标进行文本选择的应用程序,我必须自动化测试用例。

text selection

我正在考虑在文本字段中查找光标的位置,然后使用Robot mouseMove进行选择。但是仍然停留在寻找光标的位置。

    //go to start point of the selection
    Robot robot = new Robot();
    Toolkit.getDefaultToolkit().setLockingKeyState(KeyEvent.VK_NUM_LOCK, false);
    robot.keyPress(KeyEvent.VK_HOME);
    robot.delay(500);
    for (int num = 0; num < Integer.parseInt(startPoint); num++) {
        robot.keyPress(KeyEvent.VK_RIGHT);
        robot.delay(500);
        robot.keyRelease(KeyEvent.VK_RIGHT);
    }
    //find cursor position but this is for mouse position only, not position of my expected cursor in the text field
    PointerInfo a = MouseInfo.getPointerInfo();
    Point b = a.getLocation();
    int x = (int) b.getX();
    int y = (int) b.getY();
    System.out.println(x + "," + y);
    wait(3);
    for (int selection = 0; selection < text.length(); selection++) {
        robot.keyPress(KeyEvent.VK_RIGHT);
        robot.delay(500);
        robot.keyRelease(KeyEvent.VK_RIGHT);
    }
    PointerInfo c = MouseInfo.getPointerInfo();
    Point d = c.getLocation();
    int w = (int) d.getX();
    int z = (int) d.getY();
    System.out.println(w + "," + z);
    // move mouse point from the start point to end point of selection
    robot.mouseMove(x + 12, y);
    robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
    robot.delay(500);
    robot.mouseMove(w - 12, z);
    robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
    robot.delay(500);

有什么建议吗?

0 个答案:

没有答案