如何在使用java单击链接时打开一个新窗口 [英] How to open a new window while clicking on a link using java

查看:36
本文介绍了如何在使用java单击链接时打开一个新窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if(driver.findElement(By.xpath("xxx")).isDisplayed() != True){
    // if clicked in the above condition is True then it has to be opened in a new window
    driver.findElement(By.xpath("xxx")).click();
}

else {
    System.out.println("element not present -- so it entered the else loop");
}

推荐答案

您可以使用以下代码在新窗口中打开链接:

You can open link in new window using below code :

WebElement link = driver.findElement(By.xpath("your link xpath"));
Actions newwin = new Actions(driver);
newwin.keyDown(Keys.SHIFT).click(link).keyUp(Keys.SHIFT).build().perform();
Thread.sleep(6000);

通常我们按下 SHIFT 键并使用鼠标点击在新窗口中打开链接,我在这里通过 selenium 中的代码做了同样的事情.

Generally we press SHIFT key and click using mouse to open link in new window , I did same thing here via code in selenium.

这篇关于如何在使用java单击链接时打开一个新窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆