如何使用ActionSequence或LegacyActionSequence或类似方法在Javascript中链接动作?

时间:2018-08-06 16:45:29

标签: javascript node.js selenium selenium-webdriver

在执行此示例时,出现错误,我同时尝试了ActionSequence和LegacyActionSequence。我需要找到适当的方法来链接动作。我调查了 https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/actions_exports_LegacyActionSequence.html

和StackOverflow中的其他参考也无法正常工作。

我正在尝试:

var assert = require('assert');
var webdriver = require('selenium-webdriver'),
  By = webdriver.By,
  until = webdriver.until;

var chrome = require('selenium-webdriver/chrome');

var options = new chrome.Options();
options.addArguments('start-maximized');
options.addArguments('disable-popup-blocking');
options.addArguments('test-type');
var driver = new webdriver.Builder().withCapabilities(options).build();

driver.get('http://www.softpost.org/selenium-test-page/');
var e3 = driver.findElement(By.tagName('select'));

//You can use ActionSequence class to perform actions in selenium

new webdriver.LegacyActionSequence(driver)
  .keyDown(webdriver.Key.SHIFT)
  .click(e3)
  //dragAndDrop(element3, element4).
  .keyUp(webdriver.Key.SHIFT)
  .perform();

And got following error:
new webdriver.LegacyActionSequence(driver)
^

TypeError: webdriver.LegacyActionSequence is not a constructor
    at Object.<anonymous> (ActionSequenceTest.js:19:1)
    at Module._compile (internal/modules/cjs/loader.js:678:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:689:10)
    at Module.load (internal/modules/cjs/loader.js:589:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:528:12)
    at Function.Module._load (internal/modules/cjs/loader.js:520:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:719:10)
    at startup (internal/bootstrap/node.js:228:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:575:3)

1 个答案:

答案 0 :(得分:2)

LegacyActionSequence类已被使用(请参见源代码comment),因此请使用actions()

driver.actions({bridge: true}).sendKeys(webdriver.Key.SHIFT)
        .click(e3)
        .sendKeys(webdriver.Key.SHIFT)
        .perform();