开发者

Open a new browser session in a different window in Selenium

开发者 https://www.devze.com 2022-12-16 17:35 出处:网络
I am new to S开发者_开发百科elenium and just started to use it. I want to open a new browser session in a different window from my script and do not know how to do it.

I am new to S开发者_开发百科elenium and just started to use it. I want to open a new browser session in a different window from my script and do not know how to do it.

I tried using the open command and gave the Firefox Url but it opened in the same window.

Any ideas?


Try using openWindow instead of open. If you get a message that Firefox blocked a popup, allow popups. This will probably open a tab instead of a window, but maybe that will suit your needs.


@Test
public void Test01() throws Exception {
openTab("http://www.xyz.com");
}

This will open a different Firefox window. And then Handle to switch the new window.

public void trigger(String script, WebElement element) {
((JavascriptExecutor) driver).executeScript(script, element);
}

public Object trigger(String script) {
return ((JavascriptExecutor) driver).executeScript(script);
}

public void openTab(String url) {
String script = "var d=document,a=d.createElement('a');a.target='_blank';a.href='%s';a.innerHTML='.';d.body.appendChild(a);return a";
Object element = trigger(String.format(script, url));
if (element instanceof WebElement) {
WebElement anchor = (WebElement) element;
anchor.click();
trigger("var a=arguments[0];a.parentNode.removeChild(a);", anchor);
} else {
throw new JavaScriptException(element, "Unable to open Window", 1);
}
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号