public void shiftClick() {
if(WebBrowser.isInternetExplorer()) {
try {
Robot robot = new Robot();
try {
WindowManagement.setBrowserFocus();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
robot.keyPress(KeyEvent.VK_SHIFT);
this.clickElement();
robot.keyRelease(KeyEvent.VK_SHIFT);
} catch (AWTException e) {
throw new AssertionError(e.getMessage());
}
} else {
Actions actions = new Actions(Browser.getWebDriver(null));
actions.keyUp(Keys.SHIFT).click(this.getWebElement()).keyUp(Keys.SHIFT).perform();
}
}
将selenium从2.53升级到3.6.0后,转换点击不起作用。我尝试过AWT机器人方法,并尝试了Action键方法。有没有其他方法可以执行班次点击?
答案 0 :(得分:0)
在IE中使用WebDriver的提示(您可以找到一些here):
Enhanced Protected Mode
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE
requireWindowFocus
选项enableNativeEvents
,有时可以帮助 IE11 Windows 10 WebDriver 3.6.0的工作示例
对于此页面:w3schoold click whith SHIFT key pressed
其中显示了在单击元素时如何检测是否按下了SHIFT键。
WebDriver driver= null;
try{
InternetExplorerOptions opt = new InternetExplorerOptions();
// opt.enableNativeEvents();
opt.requireWindowFocus();
driver=new InternetExplorerDriver(opt);
driver.manage().window().maximize();
driver.get("https://www.w3schools.com/js/tryit.asp?filename=tryjs_dom_event_shiftkey");
final By clickOnThis = By.xpath("//p[ text() = 'Click on this paragraph. An alert box will tell you if you pressed the shift key or not.' ]");
final By message = By.xpath("//p[ text() = 'The shift key was pressed!' ]");
WebDriverWait wait = new WebDriverWait( driver, 5 );
driver.switchTo().defaultContent();
driver.switchTo().frame("iframeResult");
WebElement clickOnMe = wait.until(ExpectedConditions.visibilityOfElementLocated(clickOnThis));
Actions actions = new Actions( driver );
actions.keyDown(Keys.SHIFT).click(clickOnMe).keyUp(Keys.SHIFT);
actions.build().perform();
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(message));
System.out.println("SHIFT-cLick detected");
Thread.sleep(5000);
}finally {
if(driver!=null) {
driver.quit();
}
}