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

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));

Related

How to handle untrusted certificate in firefox using Selenium Web Driver?

I'm facing some issues while handling "Untrusted Certificate" in firefox.
We can't use FirefoxDriver(new FirefoxProfile) as it is deprecated
I used the following code but couldn't achieve it.
FirefoxProfile profile=new FirefoxProfile();
profile.setAcceptUntrustedCertificates(true);
profile.setAssumeUntrustedCertificateIssuer(false);
FirefoxOptions options=new FirefoxOptions().setProfile(new FirefoxProfile());
WebDriver driver=new FirefoxDriver(options);
driver.get("Web Link");
Could anyone suggest me the solution to achieve in Selenium 3.
Try this in Firefox
DesiredCapabilities handlSSLErr = DesiredCapabilities.firefox ();
handlSSLErr.setCapability (CapabilityType.ACCEPT_SSL_CERTS, true);
WebDriver driver = new FirefoxDriver (handlSSLErr);
driver.get("Your URL link");
For chrome
DesiredCapabilities handlSSLErr = DesiredCapabilities.chrome ();
handlSSLErr.setCapability (CapabilityType.ACCEPT_SSL_CERTS, true);
WebDriver driver = new ChromeDriver (handlSSLErr);
driver.get("Your URL link");
Below works fine for me
DesiredCapabilities cap = new DesiredCapabilities().merge(DesiredCapabilities.firefox());
cap.acceptInsecureCerts();
FirefoxDriver driverF = new FirefoxDriver(cap);
driverF.get("https://expired.badssl.com/");

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

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

I am writing a Selenium script in Firefox but I am getting "Untrusted Certificate"

written a code like this
FirefoxProfile profile=new FirefoxProfile();
profile.setAcceptUntrustedCertificates(true);
WebDriver driver=new FirefoxDriver();
driver.get("url");
Not working in firefox how to resolve this one.
You're not creating the driver with the profile - you're creating it with an empty constructor. Try WebDriver driver=new FirefoxDriver(profile);
You can use Selenium WebDriverJS
var WebDriver = require('selenium-webdriver');
var driver = new WebDriver.Builder().withCapabilities(
WebDriver.Capabilities.firefox()
).build();
driver.get('url');

How can I handle Geo Location popup in browser using selenium webdriver?

I want to share my location on click of 'Share Location' button on the popup. How can I handle this using selenium webdriver? Refer image below.
Steps to reach to Location popup:
Navigate to this URL
Click on Try it button from right section
Location popup will be displayed as :
So lets say if for launching any site, this GeoLocation pop up comes, You can't interact with this element as its not a WebElement, so you have to handle it before the browser launches a site so below are the properties you need to set to launch the browser:-
For Firefox:
FirefoxProfile geoDisabled = new FirefoxProfile();
geoDisabled.setPreference("geo.enabled", false);
geoDisabled.setPreference("geo.provider.use_corelocation", false);
geoDisabled.setPreference("geo.prompt.testing", false);
geoDisabled.setPreference("geo.prompt.testing.allow", false);
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(FirefoxDriver.PROFILE, geoDisabled);
driver = new FirefoxDriver(capabilities);
For Chrome:
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("test-type");
options.addArguments("enable-strict-powerful-feature-restrictions");
options.addArguments("disable-geolocation");
cap.setCapability(ChromeOptions.CAPABILITY, options);
cap = cap.merge(DesiredCapabilities.chrome());
driver = new ChromeDriver(cap);
Hope it helps!
FirefoxProfile geoDisabled = new FirefoxProfile();
geoDisabled.setPreference("geo.enabled", false);
geoDisabled.setPreference("geo.provider.use_corelocation", false);
geoDisabled.setPreference("geo.prompt.testing", false);
geoDisabled.setPreference("geo.prompt.testing.allow", false);
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(FirefoxDriver.PROFILE, geoDisabled);
WebDriver driver;
System.setProperty("Driver_Name", "Driver_path");
driver =new FirefoxDriver(geoDisabled);
Need to pass the instance of FirefoxProfile.

Exception in thread "main" org.openqa.selenium.NoAlertPresentException: no alert open

Using Selenium Webdriver,I tried to open fb page after logging in. Once I logged in ,there is a pop up box appearing as follows
"Show notifications
Allow and Block"
I want to select "Allow" button.
I got an error message as
Exception in thread "main"
org.openqa.selenium.NoAlertPresentException: no alert open
Please help.
Here is the code I wrote:
System.setProperty("webdriver.chrome.driver","C:\\Users\\ABCD\\Desktop\\chromedRiver.exe");
WebDriver driver=new ChromeDriver();
driver.get("http://www.facebook.com");
driver.findElement(By.xpath(".//*[#id='email']")).sendKeys("xxx");
driver.findElement(By.xpath(".//*[#id='pass']")).sendKeys("xxx");
driver.findElement(By.xpath(".//*[#id='u_0_m']")).click();
Thread.sleep(2000);
Alert alert=driver.switchTo().alert();
String msg= alert.getText();
System.out.println(msg);
Thread.sleep(2000);
alert.accept();
It is not an alert as stated by Zach, it is a browser notification, and in your case you can press the allow button just by sending the Space Key using the Robot class in Java:
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyRelease(KeyEvent.VK_SPACE);
So the code would be:
System.setProperty("webdriver.chrome.driver","C:\\Users\\ABCD\\Desktop\\chromedRiver.exe");
WebDriver driver=new ChromeDriver();
driver.get("http://www.facebook.com");
driver.findElement(By.xpath(".//*[#id='email']")).sendKeys("xxx");
driver.findElement(By.xpath(".//*[#id='pass']")).sendKeys("xxx");
driver.findElement(By.xpath(".//*[#id='u_0_m']")).click();
Thread.sleep(4000);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyRelease(KeyEvent.VK_SPACE);
Thread.sleep(2000);
I am not sure if there is a way to retrieve the notification message though as you were trying to do.