我的Selenium代码无法正确执行keyPress +点击操作。
测试应打开jqueryui.com链接并选择页面上的前2个li元素。
我正在使用Selenium 2.23和Firefox 10.我的代码如下(我有4种不同的方式让它工作但没有执行):
FirefoxProfile profile = new FirefoxProfile();
profile.setEnableNativeEvents(真); WebDriver browser = new FirefoxDriver(profile); browser.get( “http://jqueryui.com/demos/selectable/”);
List elements = browser.findElements(By.cssSelector(“ol#selectable li”));
Actions a = new Actions(browser); a.keyDown(Keys.CONTROL) .moveToElement(elements.get(0)) .click() .moveToElement(elements.get(1)) .click() .keyUp(Keys.CONTROL) .build() .perform(); Keyboard keyboard = ((HasInputDevices) browser).getKeyboard(); keyboard.pressKey(Keys.CONTROL); List<WebElement> selectOptions = browser.findElements(By.cssSelector("ol#selectable li")); selectOptions.get(1).click(); selectOptions.get(3).click(); keyboard.releaseKey(Keys.CONTROL);
Actions builder = new Actions(browser);
builder.keyDown(elements.get(0), Keys.CONTROL)
.click(elements.get(0))
.click(elements.get(1))
.keyUp(Keys.CONTROL);
Action selectMultiple = builder.build();
selectMultiple.perform();
Robot robot = new Robot();
robot.delay(1000);
robot.keyPress(KeyEvent.CTRL_MASK);
elements.get(0).click();
elements.get(1).click();
robot.keyRelease(KeyEvent.CTRL_MASK);
browser.quit();
有人可以帮我提一些其他建议吗?
答案 0 :(得分:3)
这是一个bug in Selenium,影响了shift / control / alt以及Firefox for Windows的点击。给错误加注星标,也许他们会解决它。
答案 1 :(得分:2)
我真的不知道你为什么没有尝试(特别是第一次)。关键的常数是一团糟。
无论如何,我已经能够完成这项工作(在Windows XP上):
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
elements.get(0).click();
elements.get(1).click();
robot.keyRelease(KeyEvent.VK_CONTROL);
答案 2 :(得分:1)
I think that`s not a bug.
Try use this (C#):
Action builder = new Actions(driver);
builder.KeyDown(Keys.Control);
builder.Click(element1);
builder.Click(element2);
builder.KeyUp(Keys.Control);
builder.Perform();
or for you(Java):
Actions a = new Actions(browser); a.keyDown(Keys.CONTROL) .moveToElement(elements.get(0)) .click() .moveToElement(elements.get(1)) .click() .keyUp(Keys.CONTROL) .build() .perform();
Just instead of
.Click(); .build(); .perform();
use
a.Click(YourWebElement);
a.keyUp(Keys.CONTROL);
a.build();
a.perform();
Should works,