How to switch to parent tab to the new tab and open a new URL on Chrome Browser? [duplicate]

How to switch to parent tab to the new tab and open a new URL on Chrome Browser? [duplicate]



This question already has an answer here:



How to switch to parent tab to the new tab and open a new URL on Chrome Browser?



I want to access a new URL after opening the new tab so I have written the code as below but I am able to open the new tab and the system is opening the second URL in the parent window but not on the second tab?


getDriver().get("http://www.google.com");
String EsatwindowHandle = getDriver().getWindowHandle();

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_T);

Thread.sleep(5000);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_TAB);

getDriver().get("https://in.yahoo.com");



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





Why are you mixing Selenium and Robot? I don't think it makes for clearer code. Have you googled this? What did you find?
– Robert
Aug 28 at 19:35





The above given references are to access a new tab by clicking a link from the parent tab and driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t"); //Switch between tabs using Ctrl + t driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t"); Tried this but it's not working in Chrome and when I googled most of the people faced this issue in Chrome , so only I am using the Robot
– Smart coder
Aug 28 at 19:43






any suggestion than above two refrences, I Tried all those and we can see the comments about the Chrome browser issue on these 2 refrences
– Smart coder
Aug 28 at 19:45





Please don't use comments to clarify, edit your question instead. Comments may or may not be shown initially. They do appear in order of votes. You also cannot format code properly in comments. Make it easy to help you by putting everything in one place --- the question. You may want to take the tour of the site.
– Robert
Aug 28 at 19:55




1 Answer
1



Use WebDriver getWindowHandles() and switchTo() method as below:


WebDriver


getWindowHandles()


switchTo()


WebDriver driver = getDriver();
driver.get("http://www.google.com");
String EsatwindowHandle = driver.getWindowHandle(); //get the window handler for main window

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_T);

Thread.sleep(5000);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_TAB);

//Switch to new window opened
for(String winHandle : driver.getWindowHandles())
driver.switchTo().window(winHandle);


driver.get("https://in.yahoo.com");



To switch back to previous window(parent tab), write below code at the end


driver.switchTo().window(EsatwindowHandle);





Hi Ali, logged in to Gmail and after sending an email if I want to open a new tab and Launch New URL do I need these 2 lines WebDriver driver = getDriver(); driver.get("google.com"); ? As I am already on the Gmail?
– Smart coder
Aug 29 at 10:46





ANd after login to Yahoo How to switch back to parent tab i.e gmail tab, the above code is not working
– Smart coder
Aug 29 at 10:46





@Smartcoder just comment the line //driver.get("http://www.google.com"); if you be already on the Gmail. The last line will work to switch back to parent tab (i.e gmail tap)
– Ali Azam
Aug 29 at 10:57



//driver.get("http://www.google.com");

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế