c# - Open new tab in chrome from selenium web-driver doesn't work -
code language c# selenium webdriver
i'm trying open in chrome new tab following code:
actions action = new actions(browserfactory.driver); action.sendkeys(keys.control + "t").build().perform(); string secondtabhandle = browserfactory.driver.currentwindowhandle;
i found code on stackoverflow.
i tried:
iwebelement body = browserfactory.driver.findelement(by.tagname("body")); body.sendkeys(keys.control+'t'); body.sendkeys(keys.control+"t");
thats not working
nothing happens after using code.
can me doing wrong.
thanks in advance.
the better solution not dependent press ctrl+t or whatever, because on different browser or different version on same brower, ctrl+t may lead different behaviour.
i prefer solution execute javascript on browser open new tab, becasuse inject , execute javascript on browser supported natively selenium.
we should make javascript following things on browser:
create link node, , set link href 'about:blank' or url want open, set link target '_blank'
append link node body of current opening page
click link , remove link body
code example:
string newtabscript = "var d=document,a=d.createelement('a');" + "a.target='_blank';a.href='{0}';" + "a.innerhtml='new tab';" + "d.body.appendchild(a);" + "a.click();" + "a.parentnode.removechild(a);" public void newtab(string taburl) { if(string.isnullorempty(taburl) { taburl = "about:blank"; } iwebdriver driver; // assume assigned elsewhere ijavascriptexecutor js = (ijavascriptexecutor)driver; js.executescript(string.format(newtabscript, taburl)); }
Comments
Post a Comment