在Java中现有的浏览器/标签中打开网址 [英] Open URL in existing browser/tab in java
问题描述
通过以下code,我设法打开一个URL与我的外部浏览器(在我的情况 - 火狐)。每次打开URL操作将创建一个新的标签,并随着时间的推移我有上百个标签,我想prevent的。我用这一个工具直观地检查一些日期在一个网页,当pressing接下来,它应该显示下一个页面在同一个标签。
我如何实现这一目标?
code:
公共静态无效openWebpage(网址URL){
尝试{
openWebpage(url.toURI());
}赶上(的URISyntaxException E){
e.printStackTrace();
}
}公共静态无效openWebpage(URI URI){
桌面桌面= Desktop.isDesktopSupported()? Desktop.getDesktop():空;
如果(桌面= NULL&放大器;!&安培; desktop.isSupported(Desktop.Action.BROWSE)){
尝试{
desktop.browse(URI);
}赶上(例外五){
e.printStackTrace();
}
}
}
我认为你需要使用这个webdriver的,但也许这就是矫枉过正了一点点:
http://docs.seleniumhq.org/docs/03_webdriver.jsp
样code:
=驱动新FirefoxDriver();
driver.get(http://google.com);
driver.navigate()至(http://stackoverflow.com);
最后一行将使用相同的窗口中打开新的URL。
With the following Code I managed to open an URL with my external Browser (in my case - Firefox). Every URL open action creates a new tab and over time I have hundreds of tabs, which I want to prevent. I use this for a tool to visually check some dates in a webpage and when pressing next, it should show the next page in the same tab.
How do i achieve this ?
Code:
public static void openWebpage(URL url) {
try {
openWebpage(url.toURI());
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
public static void openWebpage(URI uri) {
Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
try {
desktop.browse(uri);
} catch (Exception e) {
e.printStackTrace();
}
}
}
I think you need to use a WebDriver for this, but maybe thats a little bit of overkill:
http://docs.seleniumhq.org/docs/03_webdriver.jsp
Samplecode:
driver = new FirefoxDriver();
driver.get("http://google.com");
driver.navigate().to("http://stackoverflow.com");
the last line will use the same window to open the new url.
这篇关于在Java中现有的浏览器/标签中打开网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!