java 如何在使用java单击链接时打开一个新窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31603309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open a new window while clicking on a link using java
提问by kripindas
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");
}
回答by Helping Hands
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);
Generally we press SHIFT key and click using mouse to open link in new window , I did same thing here via code in selenium.
通常我们按下 SHIFT 键并单击使用鼠标在新窗口中打开链接,我在这里通过 selenium 中的代码做了同样的事情。
回答by Subh
You can use the below code snippet; just replace the locator with what you want, and it should work:
您可以使用以下代码片段;只需用你想要的替换定位器,它应该可以工作:
driver.get("https://www.google.co.in");
Actions act = new Actions(driver);
act.moveToElement(driver.findElement(By.xpath("//a[.='??????']"))).contextClick().sendKeys(Keys.DOWN).sendKeys(Keys.DOWN).sendKeys(Keys.ENTER).build().perform();
The above snippet navigates to Google site, then right clicks on the concerned link "??????"in this case, and uses the down key twice to reach the option of "Open link in a new window" and then sends "Enter" key to click on it, which then opens a new window.
上面的代码段导航到 Google 站点,然后右键单击相关链接“??????” 在这种情况下,使用向下键两次到达“在新窗口中打开链接”选项,然后发送“Enter”键单击它,然后打开一个新窗口。
NOTE:-This works fine in Firefox and Chrome. In case of Internet Explorer, you might have to add one extra sendKeys(keys.DOWN)
and it should be good because the option for "Open link in a new window" comes in the third place. Please check the snippet change for the same, below:
注意:-这在 Firefox 和 Chrome 中工作正常。在 Internet Explorer 的情况下,您可能需要额外添加一个sendKeys(keys.DOWN)
,这应该很好,因为“在新窗口中打开链接”选项排在第三位。请在下面检查相同的代码段更改:
act.moveToElement(driver.findElement(By.xpath("//a[.='??????']"))).contextClick().sendKeys(Keys.DOWN).sendKeys(Keys.DOWN).sendKeys(Keys.DOWN).sendKeys(Keys.ENTER).build().perform();
回答by Hubert Grzeskowiak
Yet another way is injecting JS to set the target attribute on a link:
另一种方法是注入 JS 以在链接上设置目标属性:
WebElement link = driver.findElement(By.linkText("my link"));
JavascriptExecutor js = (JavascriptExecutor) driver;
String script = "return arguments[0].target='_blank'";
Object result = js.executeScript(script, link);
link.click();
The result line can probably be ignored but I found this being more reliable.
结果行可能会被忽略,但我发现这更可靠。
By the way:
顺便一提:
1) Never compare to true or false. Instead of
1)永远不要与真或假进行比较。代替
if (condition != true)
write
写
if (! condition)
2) Do not look up the same element every time. Look for it once and save the reference.
2)不要每次都查找相同的元素。查找一次并保存参考。
3) You can't click a link that is not displayed.
3) 您不能单击未显示的链接。