java - Selenium Internet Explorer 单击按钮

标签 java internet-explorer selenium

我有一个问题。我想单击按钮并在新选项卡中打开链接。这是我的代码:

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

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

附注在 Chrome 和 Firefox 上,此代码运行良好

最佳答案

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

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();

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

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]);

关于java - Selenium Internet Explorer 单击按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36116135/

相关文章:

java - 在 Oracle 和 H2 中的 @Formula 中使用 CURRENT_DATE()

类的实例变量的Java默认值未初始化为int的零

java - 从输出中确定最大值

java - Spring MVC 主页

java - 如何使用 Selenium 识别所选项目的 Web 元素,以动态更改名称和 id

python - Selenium 2.53.5 httplib.BadStatusLine : '' Python

java - Selenium:在没有直接链接、没有 Windows 窗体、没有 AutoIt 或 Robot 的情况下,将 Internet Explorer 中的文件下载到指定文件夹

css - 第二个 CSS 类在 IE8 中不起作用

jQuery fadeIn 和 fadeOut 在 Internet Explorer 6、7 或 8 中不起作用?

python - python selenium 获取表格行文本