Selenium操作多线程

huangapple go评论69阅读模式
英文:

Selenium actions multithreading

问题

我想要打开多个Selenium WebDrivers,并在不同的线程中处理它们。问题是Selenium的Action类如何共享键盘/鼠标资源?

public class InterruptInput implements Runnable {

    public static void main(String[] args) {
        // 两个action实例将同时运行
        new Thread(new InterruptInput()).start();
        new Thread(new InterruptInput()).start();
    }

    @Override
    public void run() {
        // 不确定是否可以使用ChromeDriver复制相同的操作
        WebDriver driver = new FirefoxDriver();
        Actions act1 = new Actions(driver);
        act1.moveToElement(searchBar)
                .click()
                .sendKeys("G")
                .pause(1000)
                .sendKeys("O")
                .pause(1000)
                .sendKeys("O")
                .pause(1000)
                .sendKeys("G")
                .pause(1000)
                .sendKeys("L")
                .pause(1000)
                .sendKeys("E")
                .pause(1000)
                .perform();
    }
}

观察到的结果是:

  1. 会打开两个浏览器。每个浏览器会在7秒内向搜索栏输入GOOGLE。没有干扰。
  2. 如果我在同一页上手动点击某个地方并将搜索栏失去焦点,输入将被中断。
  3. 如果我手动在其中一个浏览器中打开一个新标签,或者在操作系统中切换到其他应用程序,GOOGLE将在后台完成输入。

如何实现这一点?这是否意味着我可以打开多个WebDriver实例,而不用担心阻塞I/O?这与Java的Robot类有何不同,后者会主动控制键盘和鼠标?供参考,我使用的是Windows 10。不确定是否与平台有关。

基本目的是,我正在编写一个简单的应用程序,允许用户(即我)在爬取一些网站的同时,当然也可以在计算机上进行其他工作。

英文:

I would like to open multiple Selenium WebDrivers and process them in different threads. The question is how do Selenium Action class share keyboard/ mouse resources?

public class InterruptInput implements Runnable {

    public static void main(String[] args) {
        // two instances of action will run simultaneously
        new Thread(new InterruptInput()).start();
        new Thread(new InterruptInput()).start();
    }

    @Override
    public void run() {
        // not sure if the same can be replicated using ChromeDriver
        WebDriver driver = new FirefoxDriver();
        Actions act1 = new Actions(driver);
        act1.moveToElement(searchBar)
                .click()
                .sendKeys("G")
                .pause(1000)
                .sendKeys("O")
                .pause(1000)
                .sendKeys("O")
                .pause(1000)
                .sendKeys("G")
                .pause(1000)
                .sendKeys("L")
                .pause(1000)
                .sendKeys("E")
                .pause(1000)
                .perform();
    }
}

The observed result is:

  1. Two browsers will open. Each will type GOOGLE to the search bar in 7 seconds. There are no interference.
  2. If I manually click somewhere on the same page, and move the search bar out of focus, the input will be interrupted.
  3. If I manually open a new tab in one of the browsers, or switch to other apps in the operating system, GOOGLE will finish typing in the background.

How is this achieved? Does this mean I can open as many instances of WebDriver, and not worry about clogging up I/O. How is this different from Java's Robot class, which actively takes over keyboard and mouse control? For the reference, I am using Windows 10. Not sure if this is platform dependent.

The fundamental purpose being, I am writing a simple app that allows the user (me) to scrape some websites, while obviously allowing the user to do other work on the computer.

答案1

得分: 0

使用Actions类时,您不会使用真实的输入设备。您唯一分享的是代码中拥有的内容 - 一个对于每个线程都是新的WebDriver对象,以确保没有干扰。

因此,通过执行“鼠标移动”,您向WebDriver发送一个命令,该命令会将事件发送到浏览器,就像如果您使用真实的鼠标或键盘一样。真实的鼠标指针不会移动。

英文:

When you use Actions class you do not use real input devices. The only thing you share is what you have in your code - a WebDriver object that is new for each of your threads so that there is no interference.

So by performing a "mouse move" you send a command to webdriver that sends the event to a browser that it would receive if you would use the real mouse or keyboard. The real mouse pointer would not move.

huangapple
  • 本文由 发表于 2020年10月14日 15:48:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64348812.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定