I can't open anything with Selenium WebBrowser Java - java

I am trying to open local files with Selenium. With the code below, Firefox is opening, but I have the error org.openqa.selenium.WebDriverException: Timed out waiting 45 seconds for Firefox to start..
File gecko = new File("resources/geckodriver64.exe");
System.setProperty("webdriver.gecko.driver", gecko.getAbsolutePath());
FirefoxOptions capabilities = new FirefoxOptions();
capabilities.setCapability("marionette", false);
WebDriver driver = new FirefoxDriver(capabilities);
driver.get("file:///C:/example/myfile.pdf");
Can someone help me ? I couldn't find anything on the internet.

We have now come to the part where you will see how you can use GeckoDriver to launch Firefox. You will first need to download GeckoDriver and then set its path. There are three different ways to use GeckoDriver with Selenium 3:
With setting system properties in the test
With setting system properties by Environment Variable
With setting up Browser Desired Capabilities
Download Gecko Driver:-
1- Gecko Driver different versions can be downloaded from Github. I suggest you to use the latest version.
Set System Properties for Gecko Driver:-
Code to set the System properties is System.setProperty(“webdriver.gecko.driver”,”Path to geckodriver.exe”);
The complete program to launch the GeckoDriver will be like this:
package seleniumPrograms;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Gecko_Driver {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver", "D:\\\\XXXX\\trunk\\Library\\drivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.toolsqa.com");
Thread.sleep(5000);
driver.quit();
}
}

Check Below answer. This is working solution on my machine. Please check your firefox version too.
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class geckodriver {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.gecko.driver", "C:\\Users\\username\\Downloads\\geckodriver-v0.20.1-win64\\geckodriver.exe");
Thread.sleep(5000);
// DesiredCapabilities capabilities = DesiredCapabilities.firefox();
// capabilities.setCapability("marionette", true);
//
// WebDriver driver = new FirefoxDriver(capabilities);
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities = DesiredCapabilities.firefox();
capabilities.setBrowserName("firefox");
capabilities.setVersion("your firefox version");
capabilities.setPlatform(Platform.WINDOWS);
capabilities.setCapability("marionette", false);
WebDriver driver = new FireFoxDriver(capabilities);
driver.get("http://www.google.com");
Thread.sleep(5000);
driver.quit();
}}

Can you try below code ?
package seleniumPrograms;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class Gecko_Driver {
public static void main(String[] args) throws InterruptedException {
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
WebDriver driver = new FirefoxDriver(capabilities);
driver.get("http://www.google.com");
Thread.sleep(5000);
driver.quit();
}

Related

Intellij Idea cannot resolve constructor

My problem is Firefox. I installed a different location. But I tried this solution isn't working for me.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxProfile;
import java.io.File;
public class firefoxDriver
{
public static void main(String[] args)
{
File pathBinary = new File("D:\\Program Files\\Mozilla Firefox\\firefox.exe");
FirefoxBinary ffBinary = new FirefoxBinary(pathBinary);
FirefoxProfile ffProfile = new FirefoxProfile();
// Add .exe file
System.setProperty("webdriver.gecko.driver", "D:\\Docs\\Drivers\\geckodriver.exe");
// Create Firefox object driver.
WebDriver ffDriver = new FirefoxDriver(ffBinary, ffProfile);
ffDriver.get("https://google.com");
System.out.println(ffDriver.getTitle());
}
}
The error I get:
N.B - In your particular example you do not need to create a profile. New profile is created automatically by default. You use profiles when you have some pre-configured profiles persisted before you run your driver.
You can do this:
FirefoxOptions options = new FirefoxOptions();
options.setBinary(new FirefoxBinary(new File("...")));
options.setProfile(new FirefoxProfile(new File("...")));
WebDriver = new FirefoxDriver(options);
Another way to specify browser binary isto use system property:
System.setProperty(
FirefoxDriver
.SystemProperty.BROWSER_BINARY, "PATH_TO_YOUR_BINARY");
If you have a named profile set up for your instance of firefox browser, then you should do the following (like in previous case before you create a driver):
System.setProperty(
FirefoxDriver
.SystemProperty.BROWSER_PROFILE, "NAME_OF_PROFILE");
There is not constructor that is overloaded to have binary and profile together. You should use options.
Code :
System.setProperty("webdriver.gecko.driver", "D:\\Docs\\Drivers\\geckodriver.exe");
String pathBinary = "D:\\Program Files\\Mozilla Firefox\\firefox.exe";
FirefoxOptions ffOptions = new FirefoxOptions();
FirefoxProfile ffProfile = new FirefoxProfile(pathBinary);
ffOptions.setProfile(ffProfile);
ffOptions.setBinary("C:\\Program Files\\Mozilla Firefox 52\\firefox.exe");
// Create Firefox object driver.
WebDriver ffDriver = new FirefoxDriver(ffOptions);
ffDriver.get("https://google.com");
System.out.println(ffDriver.getTitle());
public static void main(String[] args)
{
// Add .exe file
System.setProperty("webdriver.gecko.driver", "D:\\Docs\\Drivers\\geckodriver.exe");
File pathBinary = new File("D:\\Program Files\\Mozilla Firefox\\firefox.exe");
FirefoxBinary ffBinary = new FirefoxBinary(pathBinary);
DesiredCapabilities desired = DesiredCapabilities.firefox();
FirefoxOptions ffOptions = new FirefoxOptions();
desired.setCapability(FirefoxOptions.FIREFOX_OPTIONS, ffOptions.setBinary(ffBinary));
// Create Firefox object driver.
WebDriver ffDriver = new FirefoxDriver(ffOptions);
ffDriver.get("https://google.com");
System.out.println(ffDriver.getTitle());
}
I solved my problem with this method. I don't know there is a much more simple solution.

IOSDriver can't resolve constructor, wants to cast DesiredCapabilities to Capabilities

package AppiumTest;
import java.net.URL;
import java.net.MalformedURLException;
import org.testng.annotations.Test;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.CapabilityType;
import io.appium.java_client.ios.IOSDriver;
public class TestIOSSafariBrowser{
#Test
public void startBrowser() throws MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName","iPhone 8");
capabilities.setCapability("platformName", "iOS");
capabilities.setCapability("platformVersion", "11.4.1");
capabilities.setCapability(CapabilityType.BROWSER_NAME, "safari");
IOSDriver driver = new IOSDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.get("http://www.google.com");
System.out.println("Page title is " + driver.getTitle());
}
}
I'm relatively new to using Selenium and Appium.
I'm trying to open Safari to access Google through Xcode and Appium. When I instantiate IOSDriver as driver down below, IOSDriver does not allow me to pass in a DesiredCapabilities object (capabilities), instead expecting me to pass a Capabilities object. How would I try to open Safari and access the Google homepage if I use the Capabilities object instead of DesiredCapabilities, or is there another way to go about this?

Unable to Launch chrome (version 67) window 7 64 bit

Not able to launch my chrome browser(version 67) , please refer below mentioned code and screenshot
package FirstPackage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.chrome.ChromeDriver;
public class FirstClass {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\Users\\XY56082\\Desktop\\sel\\hromedriver\\chromedriver.exe");
//FirefoxDriver driver = new FirefoxDriver();
WebDriver driver = new ChromeDriver();
//driver.get("http://www.google.com");
// Open Google
driver.get("http://www.google.com");
// Close browser
driver.close();
}
}
May be you should change your driver to:
chromedriver-2.35.exe
with chrome 67 it is the perfect driver.
I am using selenium 3.8.1 in maven.
Hope that helps you.

Browser-mob Proxy is not capturing the network traffic with chrome driver

I am using this code to integrate the browser mob proxy with maven dependency
net.lightbody.bmp browsermob-core 2.1.5
its not capturing the network requests at all, I am getting this kind of har file:
{
"log":{
"version":"1.2",
"creator":{
"name":"BrowserMob Proxy",
"version":"2.1.0-beta-6-littleproxy",
"comment":""
},
"pages":[
{
"id":"11",
"startedDateTime":"2017-10-26T17:28:42.501+05:30",
"title":"11",
"pageTimings":{
"comment":""
},
"comment":""
}
],
"entries":[],
"comment":""
}
}
package lenskart.tests;
import java.io.File;
import java.net.UnknownHostException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;
import net.lightbody.bmp.BrowserMobProxyServer;
import net.lightbody.bmp.client.ClientUtil;
import net.lightbody.bmp.mitm.manager.ImpersonatingMitmManager;
import net.lightbody.bmp.proxy.CaptureType;
public class ProxyTestClass {
#Test
public static void main() throws Exception {
// TODO Auto-generated method stub
BrowserMobProxyServer browserMobProxy = new BrowserMobProxyServer();
browserMobProxy.setTrustAllServers(true);
browserMobProxy.setMitmManager(ImpersonatingMitmManager.builder().trustAllServers(true).build());
browserMobProxy.start(0);
System.out.println("Port Started On: " + browserMobProxy.getPort());
System.setProperty("webdriver.chrome.driver", "/Users/pankaj.katiyar/Desktop/Automation/Lenskart_Automation/tpt/drivers/chromedriver");
browserMobProxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT, CaptureType.RESPONSE_HEADERS);
WebDriver driver = getDriver_CapProxy(browserMobProxy);
driver.get("http://www.lenskart.com");
driver.navigate().to("http://www.google.com");
driver.quit();
browserMobProxy.stop();
browserMobProxy.newHar("11");
browserMobProxy.getHar().writeTo(new File("/Users/pankaj.katiyar/Desktop/Automation/Lenskart_Automation/har"));
;
System.out.println("Loaded browser ");
}
public static WebDriver getDriver_CapProxy(BrowserMobProxyServer browserMobProxy) throws UnknownHostException {
Proxy proxy = ClientUtil.createSeleniumProxy(browserMobProxy);
proxy.setHttpProxy("localhost:" + browserMobProxy.getPort());
DesiredCapabilities cap = new DesiredCapabilities();
ChromeOptions options = new ChromeOptions();
options.addArguments("--ignore-certificate-errors");
cap.setCapability(ChromeOptions.CAPABILITY, options);
cap.setCapability(CapabilityType.PROXY, proxy);
WebDriver driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
return driver;
}
}
There's two issues with your code.
First the selenium proxy is assigned to the capabilities, but the instance is never used. So either assign the options to the capabilities, or directly assign the selenium proxy to the options:
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(browserMobProxy);
ChromeOptions options = new ChromeOptions();
options.addArguments("--ignore-certificate-errors");
options.setCapability(CapabilityType.PROXY, seleniumProxy);
WebDriver driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Second, you are calling browserMobProxy.newHar at the end of the recording when it should be at the beginning:
browserMobProxy.newHar("11");
driver.get("http://www.lenskart.com");
driver.navigate().to("http://www.google.com");
browserMobProxy.getHar().writeTo(new File("/Users/pankaj.katiyar/Desktop/Automation/Lenskart_Automation/har"));
driver.quit();
browserMobProxy.stop();
I ran your code and only two changes you need are, change
WebDriver driver = new ChromeDriver(options);
to
WebDriver driver = new ChromeDriver(cap);
And move browserMobProxy.newHar("11"); before the navigate
browserMobProxy.newHar("11");
driver.get("http://www.lenskart.com");
Rest all is fine in your code. Once you do that Har is generated fine as shown in below screenshot

Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system

Following previous related issues posted and given resolution, I tired everything but still getting same error for FireFox, Chrome & Internet Explorer.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Search {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new FirefoxDriver();
System.getProperty("webdriver.gecko.driver",
"C:\\Users\\nitin\\Downloads\\geckodriver-v0.18.0-
win64\\geckodriver.exe");
driver.get("http://www.wikipedia.org");
WebElement link;
link = driver.findElement(By.linkText("English"));
link.click();
Thread.sleep(5000);
WebElement searchbox;
searchbox = driver.findElement(By.id("searchInput"));
searchbox.sendKeys("Software");
searchbox.submit();
Thread.sleep(5000);
driver.quit();
Shouldn't that be System.setProperty() instead of .getProperty()?
System.setProperty("webdriver.gecko.driver, "C:\\Users\\...\\geckodriver.exe");
Use that gecko driver system property before driver intialization
So first line gecko property and next line driver=new so and so..
Use .setProperty and declare it after providing the path to webdriver
System.setProperty("webdriver.gecko.driver",
"C:\\Users\\nitin\\Downloads\\geckodriver-v0.18.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();

Categories