使用Selenium记录操作

use*_*114 10 firefox selenium action record

关于Selenium,我有一个半模糊的问题.我发现了一些使用FirefoxDriver执行操作的不同方法.我需要做的是重复用户在网页上执行的操作(单击链接,选中复选框等).是否有任何方法或方法组合允许我"记录"用户的行为?以下是我到目前为止执行操作的内容(您会注意到我已尝试使用WebDriverBackedSelenium和Actions类来执行操作)

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.Action;

public class MyReplayer {
    public static void main(String[] args) throws Exception {
        // The Firefox driver supports javascript 
        FirefoxDriver driver = new FirefoxDriver();

        driver.get("http://www.cs.umd.edu");

        List<WebElement> elements = driver.findElements(By.tagName("a"));
        //WebDriverBackedSelenium driverBacked = new WebDriverBackedSelenium(driver,        "http://www.cs.umd.edu");
        Actions builder = new Actions(driver);    
        Action clickLink = builder.click(elements.get(100)).build();
        clickLink.perform();
        //driverBacked.click("document.getElementsByTagName('a')[100]");
     }
}
Run Code Online (Sandbox Code Playgroud)

小智 8

我遇到了赫胥黎.它允许记录和回放用户操作.我发现这个问题是为了寻找他们是如何做到的,但不得不求助于源代码.

huxley/run.py的第98-154行定义了记录功能.它使用webdirvier在页面上执行一些js,这会添加一些事件监听器.它还添加了一个返回事件的函数.

(function() {
var events = [];
window.addEventListener('click', function (e) { events.push([Date.now(), 'click',  [e.clientX, e.clientY]]); }, true);
window.addEventListener('keyup', function (e) { events.push([Date.now(), 'keyup', String.fromCharCode(e.keyCode)]); }, true);
window._getHuxleyEvents = function() { return events; };
})();
Run Code Online (Sandbox Code Playgroud)

要读取事件,调用js函数

events = d.execute_script('return window._getHuxleyEvents();')
Run Code Online (Sandbox Code Playgroud)

然后事件以看似特定于应用程序的方式存储.

对不起,我没有Java代码.我希望这有帮助.


ma *_*lay 5

您可以使用Selenium IDE Addon for Firefox并导出生成的Webdriver测试.它没有具体说明FirefoxDriver,但界面的方法看起来与你发布的类似.我希望这有帮助.