繁体   English   中英

Selenium Internet Explorer单击按钮

[英]Selenium Internet Explorer click button

我有个问题。 我想单击按钮并在新标签页中打开链接。 这是我的代码:

Actions actions = new Actions(this.driver);
actions.keyDown(Keys.CONTROL).click(myButton).keyUp(Keys.CONTROL).perform();

但是,链接是在当前选项卡而不是新选项卡上打开的。 这是那个虫子吗?

chrome和firefox上的PS,此代码工作得很好

您的代码很好,但IEDriver却不如FireFox和Chrome的那么好。 我有类似的问题,您可以在这里阅读。 我最终用java.awt.Robot解决了我的问题。 在您的情况下,这意味着您需要一种特定于IE的方法才能在新标签页中打开。

Robot r = null;
try {
    r = new Robot();
} catch (AWTException e) {
    e.printStackTrace();
}
...
r.setAutoDelay(30);
r.keyPress(KeyEvent.VK_CONTROL);

//then click the button

r.keyRelease(KeyEvent.VK_CONTROL);

它不优雅,但可以。

一种解决方案是在单击链接之前打开一个新窗口:

WebDriver driver = new FirefoxDriver();
driver.get("http://stackoverflow.com");
// open a new window
((JavascriptExecutor)driver).executeScript("window.open(window.location.href, '_blank');");
// set the context to the new window
driver.switchTo().window(driver.getWindowHandles().toArray()[1]);
// click the link
driver.findElement(By.linkText("Stack Overflow")).click();

另一种解决方案是在目标元素上设置目标属性:

WebDriver driver = new FirefoxDriver();
driver.get("http://stackoverflow.com");
WebElement element = driver.findElement(By.linkText("Stack Overflow"));
// set the target _blank on the link
((JavascriptExecutor)driver).executeScript("arguments[0].target='_blank';", element);
// click the link
element.click();
// set the context to the new window
driver.switchTo().window(driver.getWindowHandles().toArray()[1]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM