I have tried multiple actions but it is not working. It always close the first tab and then it open link in the same page.
// Driver is launched
package WebBrowserCommands;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class OpenBrowser
{
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver","/Users/ss108/Desktop/Util/SELENIUM SUPPORT/chromedriver");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
// Driver is launched
//We have two ways
boolean Indriectway = true;
//Boolean to understand both method
if(!Indriectway){
//Using String to take value.
String URL = "https://www.google.com";
driver.get(URL);
String PageSourceFirst = driver.getPageSource();
//We have took title in PageSourceFirst
System.out.println(PageSourceFirst);
//Get Title Command
String TitleFrist = driver.getTitle();
//We have took title in TitleFirst
System.out.println("Tile of WebSite is " + TitleFrist);
//Get Current URL
String CurrentURLFirst = driver.getCurrentUrl();
System.out.println("Current URL is " + CurrentURLFirst);
Actions act=new Actions(driver);
act.contextClick(driver.findElement(By.linkText("Gmail"))).perform();
Thread.sleep(10000);
act.contextClick(driver.findElement(By.linkText("Gmail"))).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
// Try to open page using right click and action arrow down **but it is not working**
driver.close();
}else {
//Using directly
driver.get("https://www.facebook.com");
String PageSourceSecond = driver.getPageSource();
System.out.println(PageSourceSecond);
//Get Title Command
System.out.println("Tile of WebSite is " + driver.getTitle());
//Get Current URL
System.out.println("Current URL is " + driver.getCurrentUrl());
driver.quit();
}
}
}
Second method 1- Same result getting
System.setProperty("webdriver.chrome.driver","/Users/ss108/Desktop/Util/SELENIUM SUPPORT/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://www.facebook.com")
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_META);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_META);
driver.get("https://www.google.com")
Third method
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN);
driver.findElement(By.linkText("urlLink")).sendKeys(selectLinkOpeninNewTab);
however none of them work for what I am trying.