14143/how-open-link-new-tab-chrome-browser-using-selenium-webdriver
System.setProperty("webdriver.chrome.driver", "D:\\softwares\\chromedriver_win32\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.manage().window().maximize(); driver.get("https://mail.google.com/"); String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN); driver.findElement(By.linkText("www.facebook.com")).sendKeys(selectLinkOpeninNewTab);
Below code worked fine for me:
driver = new ChromeDriver(); driver.manage().window().maximize(); baseUrl = "http://www.google.co.uk/"; driver.get(baseUrl); driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t"); ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles()); driver.switchTo().window(tabs.get(1)); //switches to new tab driver.get("https://www.facebook.com"); driver.switchTo().window(tabs.get(0)); // switch back to main screen driver.get("https://www.news.google.com");
For further understanding, you can refer to the Selenium online training.
This code is not working for me
Hey Sonal, try this piece of code to open a link in a new tab:
driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get("https://www.google.com/"); String a = "window.open(link,'_blank');"; // replace link with your desired link ((JavascriptExecutor)driver).executeScript(a);
Hope this would help you. Let me know if it works.
driver.switchTo().window(tabs.get(0)); // switch to main screen //execute your code. driver.switchTo().window(tabs.get(1)); //switches to new tab
driver.execute_script('''window.open("http://google.com","_blank");''')
First open a new tab:
driver.find_element_by_tag_name('body').send_keys(Keys.COMMAND + 't')
Then open the url using get:
driver.get('www.facebook.com')
ActionChains(driver).key_down(Keys.CONTROL).send_keys('t').key_up(Keys.CONTROL).perform()
You can open a new tab first. And then change the focus to the newly created tab
driver.execute_script("window.open('');") driver.switch_to.window(driver.window_handles[1]) driver.get("www.facebook.com")
driver.switchTo().window(tabs.get(1)); or javascrpirt
or
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
Try this once
driver.findElement(By.id("id")).sendKeys(Keys.CONTROL+"t");
This below code works for me in Selenium 3 and chrome version 58.
WebDriver driver = new ChromeDriver(); driver.get("http://yahoo.com"); ((JavascriptExecutor)driver).executeScript("window.open()"); ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles()); driver.switchTo().window(tabs.get(1)); driver.get("http://google.com");
Hi Utkarsh, you can use JS Executor ...READ MORE
Hi , we have inbuilt method Maximize(). driver.Manage().Wind ...READ MORE
Hey Uday, you can write following lines ...READ MORE
Hi Mugdha, you can use following code ...READ MORE
The better way to handle this element ...READ MORE
enable trusted connection in internet explorer by ...READ MORE
To Allow or Block the notification, access using Selenium and you have to ...READ MORE
xpath are two types. 1) Absolute XPath: /html/b ...READ MORE
You can try the below code: driver = ...READ MORE
The current version of Selenium do not ...READ MORE
OR
At least 1 upper-case and 1 lower-case letter
Minimum 8 characters and Maximum 50 characters
Already have an account? Sign in.