简体   繁体   English

如何执行移动鼠标和点击动作?

[英]How to perform move mouse and click action?

My objective is to move the mouse to我的目标是将鼠标移动到

1) scrollbar zone and 1) 滚动条区域和
2) click so that scrollbar will move down. 2) 单击使滚动条向下移动。

I am using the Robot class to perform the move mouse operation, but not able to click on the scrollbar zone.我正在使用 Robot 类来执行移动鼠标操作,但无法单击滚动条区域。

Robot rb=new Robot();
rb.mouseMove(1135,400);
Thread.sleep(5000);
Actions act=new Actions(driver);
act.click().perform();

Please help me in resolving the issue.请帮助我解决问题。

Ok, so here it is a very general demo.好的,这是一个非常通用的演示。 You can easily adapt it to your needs.您可以轻松地使其适应您的需求。 If you want to use different heights in the scroll, you have to take into account that the time (one second) I used would be greater the higher the scroll pane.如果要在滚动中使用不同的高度,则必须考虑到滚动窗格越高,我使用的时间(一秒)就越长。 However, for a generic one you can detect when the scroll pane is a the bottom and no longer call the timer.但是,对于通用的,您可以检测滚动窗格何时位于底部并且不再调用计时器。 I commented the part you can use to make it generic.我评论了您可以用来使其通用的部分。

import java.util.Timer;
import java.util.TimerTask; 
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import java.awt.Dimension;

import java.awt.Robot;
import java.awt.event.InputEvent;

public class Stackoverflow extends JFrame{
    private java.util.Timer timer;
    private JFrame window;
    public static void main(String [] args){
        new Stackoverflow();
    }
    public Stackoverflow(){
        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(500, 2000));
        panel.setOpaque(false);


        this.window = this;
        this.timer = new java.util.Timer();
        timer.schedule(new AutoSaveTasker(), 1000);         

        this.add(new JScrollPane(panel));
        this.pack();
        this.setVisible(true);
    }
    class AutoSaveTasker extends TimerTask{
        @Override
        public void run(){
            /*
            if(scroll not at the bottom yet?) 
                then call timer again like this /timer.schedule(new AutoSaveTasker(), INTERVAL);
            */
            try{

                Robot robot = new Robot();
                robot.mouseMove(window.getWidth() - 10, window.getHeight() - 10);
                robot.mousePress(InputEvent.BUTTON1_MASK);
                Thread.sleep(1000);
                robot.mouseRelease(InputEvent.BUTTON1_MASK);
            }
            catch(Exception e){
                e.printStackTrace();
            }

        }
    }
}

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

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