Skip to content

Instantly share code, notes, and snippets.

@yucer
Last active August 8, 2017 10:15
Show Gist options
  • Save yucer/860ef96b940aaebfe3da32c0f7f0f96a to your computer and use it in GitHub Desktop.
Save yucer/860ef96b940aaebfe3da32c0f7f0f96a to your computer and use it in GitHub Desktop.
Open new tab with selenium in Java (plus profile and capabilities)
package automationFramework;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.logging.LoggingPreferences;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public class FirsttestCase {
/**
* @param args
* @throws InterruptedException
* @throws IOException
*/
public static void main(String[] args) throws InterruptedException, IOException {
BufferedReader buffer=new BufferedReader(new InputStreamReader(System.in));
System.setProperty("webdriver.firefox.bin", "/home/yucer/Downloads/softlib/firefoxes/2017-08-05-10-03-34-mozilla-central-firefox-57.0a1/firefox");
FirefoxOptions options = new FirefoxOptions();
options.setLogLevel(Level.FINEST);
options.addPreference("browser.link.open_newwindow", 3);
options.addPreference("browser.link.open_newwindow.restriction", 0);
FirefoxDriver driver = new FirefoxDriver(options);
System.out.print("Openning about window.");
driver.get("about:");
Thread.sleep(4000);
driver.get("http://www.google.com");
System.out.print("Selecting the element.");
// Thread.sleep(5000);
// WebElement element = driver.findElement(By.tagName("body"));
WebElement element = driver.findElement(By.id("lst-ib"));
System.out.print("Opening new Tab");
// Thread.sleep(5000);
driver.executeScript("window.open('about:blank','_blank');");
// element.sendKeys(Keys.CONTROL, "t");
Thread.sleep(7000);
System.out.println("\nNew tab should be open now.\n");
System.out.print("Closing the browser");
driver.quit();
Thread.sleep(5000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment