Selenium : New tab is not getting opened on same browser in Chrome - selenium

I'm trying to open a new tab in same browser but it doesn't seem to work. I'm using Chrome Version 58.0.3029.110 (64-bit) and Selenium 3.0.0.
I used the below code:
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "\t");

try using JavascriptExecutor as below:
((JavascriptExecutor) driver).executeScript("window.open('https://www.google.com');");

You can also use Robot class with Selenium Webdriver to open a new tab. We need to follow the below three steps-
Simulate pressing of Ctrl+t keys of keyboard using Robot class.
Switch to the new tab in selenium using driver.switchTo() command.
Open the desired link on new tab.
Code snippet-
//Launch the first URL
driver.get("http://www.google.com");
//Use robot class to press Ctrl+t keys
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_T);
//Switch focus to new tab
ArrayList<String> tabs = new ArrayList<String (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
//Launch URL in the new tab
driver.get("http://google.com");
Source: Code snippet from Open a new tab in Selenium - ArtOfTesting

Related

Selenium IE Driver opens link on a new window and not on new tab

I am using IE11 for my selenium tests. I Am clicking on a link which when done manually opens up the link on a new tab.
When I am trying to do the same thing with Selenium, it does not open the link on a new tab rather, it opens up on a new window.
Is there a way to force IE to click and open the link on a new tab?
I am able to achieve the same with Firefox and Chrome, but with IE, as soon as the link is clicked, it opens on a new window rather than a new tab.
if (BROWSER_TYPE.equals("IE")) {
Utility.log(this.getClass().getName(),Level.INFO,"Browser type is IE downloading firefox driver is required.");
WebDriverManager webDriverManagerObject = WebDriverManager.iedriver();
webDriverManagerObject.targetPath(DRIVER_DOWNLOAD_PATH);
if(!DRIVER_VERSION.equals("latest")) {
WebDriverManagerObject = webDriverManagerObject.version(DRIVER_VERSION);
}
Utility.log(this.getClass().getName(),Level.DEBUG,"Launching IE Browser");
webDriverManagerObject.setup();
driver = new InternetExplorerDriver();
}
Expected Result: Clicking on the Link should open the page on a new tab on the same window in IE
Actual result: Clicking on the Link opens the page on a new window rather than a new tab.

How to press (Ctrl+T) in selenium-webdriver on Ubuntu (Firefox)?

I want to open a new tab in an existing browser, but in ubuntu(os) with firefox, it's not working.
I have tried both actions and robot class but still, I am unable to do it.
driver.get("http://www.google.com/");
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
System.out.println(driver.getTitle());
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_CONTROL);
Also, have tried the below code,
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
driver.manage().Timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
System.out.println(driver.getTitle());
Actions act = new Actions(driver);
act.keyDown(Keys.CONTROL).sendKeys("t").keyUp(Keys.CONTROL).build().perform();
driver.get("http://www.bing.com/");
System.out.println(driver.getTitle());
act.keyDown(Keys.CONTROL).sendKeys("t").keyUp(Keys.CONTROL).build().perform();
driver.get("http://www.yahoo.com/");
System.out.println(driver.getTitle());
After using both the above code still, a new tab is not getting opened.
Can anyone help me with this??
You could use JavaScript Executor to open a new tab.
((JavascriptExecutor) driver).executeScript("window.open()");
List<String> tabs = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));

Javascriptexecutor is not working while running through safari webdriver

I want to perform a click using javascriptexecutor.
JavasciptExecutor jse=(JavascriptExecutor) webDriver;
jse.executescript("arguments[0].click();",webelement)
While running from safari,it is showing "undefined is not a function<evaluating arguments[0].click "
Same works fine in chrome driver.
Am I need to add any capabilities while initiating webDriver?
I have already added capabilities.SetJavascriptEnabled(true). Still it is not working
You have to set the capabilities of driver browserwise.
When running the testcases in safari browser set a global flag for browser. You can create a config file and add "safari" or "chrome" when running it through safari or chrome.
In your source initialize your driver for safari like below :
if (browser.equals("safari")) {
SafariOptions safariOptions = new SafariOptions();
safariOptions.setUseCleanSession(true);
safariOptions.setUseTechnologyPreview(true);
driver = new SafariDriver(safariOptions);
}
Then navigate to your desired url. Here i am adding a sample source for your understanding.
driver.get("https://www.google.com");
WebElement elem = driver.findElement(By.className("gb_P"));
jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].click();", elem);
Hope this will solve your issue. Feel free to comment here for more information.

How to automatically close firebug tab when launching selenium firefox webdriver with firebug?

I have included firebug when launching firefox driver and it works very fine with latest selenium web driver 3.0 but meanwhile it also opens new firebug tab every time when launching browser.
As code says, i have included firebug file and added this extension in created profile. Is there any way to close the firebug tab automatically after launching the browser? If there is no automatic way then i need to use tweak to close window named "Firebug" right?
Code:
File file = new File("./firebug-2.0.17-fx.xpi");
System.setProperty("webdriver.gecko.driver", config.getStringProperty("geckodriver.path"));
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension(file);
capabilities.setCapability(FirefoxDriver.PROFILE, profile);
capabilities.setCapability("marionette", true);
webDriver = new FirefoxDriver(capabilities);
You can set the "showFirstRunPage" flag to "false" in your porfile.
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("extensions.firebug.showFirstRunPage", false);
Here is a link to all firebug preferences you can set.
Firebug Preferences
An other solution is to set the "currentVersion" to a big number like this.
profile.setPreference("extensions.firebug.currentVersion", "999");
I prefer the first on ;)
I was looking for automatically closing Firebug window but i think its not possible so posting an answer to handle opened windows after launching firebox browser with firebug capabilities, unfortunately you need to deal with extra lines of code due to this issue :)
Below solution it works fine, just use it like below:
Working code:
File file = new File("./firebug-2.0.17-fx.xpi");
System.setProperty("webdriver.gecko.driver", config.getStringProperty("geckodriver.path"));
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension(file);
capabilities.setCapability(FirefoxDriver.PROFILE, profile);
capabilities.setCapability("marionette", true);
webDriver = new FirefoxDriver(capabilities);
// Close the firebug window
Thread.sleep(4000); // Firebug window takes time to open it
Set <String> windows = webDriver.getWindowHandles();
String mainwindow = webDriver.getWindowHandle();
for (String handle: windows) {
webDriver.switchTo().window(handle);
if (!handle.equals(mainwindow)) {
webDriver.close();
}
}
webDriver.switchTo().window(mainwindow);

Selenium- Popup got blocked if I clicked Link using Javascript Executor in Chrome browser

Problem Description - Upon clicking a link on the page, popup got blocked automatically.
file = new File("tools/chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
caps.setCapability("ignoreZoomSetting", true);
caps.setCapability("nativeEvents", false);
driver = new ChromeDriver(caps);
Environment used – Selenium WebDriver – 2.43.0 ChromeDriver, Windows 7
Note – it is working fine on Firefox & IE, this issue is happening on Chrome only.
Please assist on this.
This is a Chrome Option to disable all popups for the site
Open Chrome.
Find a page that has pop-ups blocked for you.
At the end of the address bar, click the pop-up blocker icon chrome op-up
locked.
Click the link for the pop-up window you'd like to see.
To always see pop-ups for the site, select Always show pop-ups from [site].
Once you have this set use the profile it is saved against to load for the test
Another option is to open the site and Shift F5 to do a Cache Refresh
Load a Profile. The code below is C# and you haven't specified a language. Please see the links provided for Java examples
ChromeOptions options = new ChromeOptions();
userDataPath = "C:/Users/user_name/AppData/Local/Google/Chrome/User Data";
options.AddArguments("user-data-dir=" + userDataPath);
options.AddArguments("--start-maximized");
driver = new ChromeDriver("pathToChromeDriver", options);
You can pass the chromeOption to allow pop-up as shown in below:
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_setting_values.popups", 1);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(options);