How to open chrome in Selenium webdriver? - java

I'm trying to launch chrome with Selenium Webdriver and used the following code:
System.setProperty("webdriver.chrome.driver",
"C:/Program Files (x86)/Google/Chrome/Application/chrome.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.yahoo.com");
Chrome browser opens but is not proceeding further. What could be the reason for the following error:
Exception in thread "main" org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.

You are incorrectly starting up the driver
webdriver.chrome.driver is supposed to be the path to the driver that you've downloaded and not Chrome's physical location.

First you need to download chrome driver file from this link and than import its JAR to the package in eclipse.
Download the link from here
Then you will have to import it in your program.
import org.openqa.selenium.chrome.ChromeDriver;
and than make a driver instance
driver = new ChromeDriver();
Download the external JAR of chrome
In eclipse :: right click on the respective package(in the package explorer) and click on the properties. Go to Java build path and add external jars. Now add the jar file of chrome . and than follow the steps i wrote in the ans which was to import the chrome driver and creating an instance
Follow these steps in the photograph.
1)
select your file from here and right click

Below snippet shows how you can open chrome browser using selenium webdriver.
public static void main(String[] args) {
//Creating a driver object referencing WebDriver interface
WebDriver driver;
//Setting the webdriver.chrome.driver property to its executable's location
System.setProperty("webdriver.chrome.driver", "/lib/chromeDriver/chromedriver.exe");
//Instantiating driver object
driver = new ChromeDriver();
//Using get() method to open a webpage
driver.get("https://stackoverflow.com");
//Closing the browser
driver.quit();
}

Use the latest versions of ChromeDriver.
Source|
http://chromedriver.storage.googleapis.com/index.html

You need to setup your browser settings first. Try below-mentioned code if it helps:
public void setup ()
{
System.setProperty("webdriver.chrome.driver", "C:\\**PATH**\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
options.addArguments("start-maximized");
options.addArguments("--js-flags=--expose-gc");
options.addArguments("--enable-precise-memory-info");
options.addArguments("--disable-popup-blocking");
options.addArguments("--disable-default-apps");
options.addArguments("test-type=browser");
options.addArguments("disable-infobars");
driver = new ChromeDriver(options);
driver.manage().deleteAllCookies();
}
You'll need to import files by hovering on error lines.

Related

Launching of GC when Selenium basic program is run

I am running the following code in Eclipse but my Google Chrome is not launching.
The configurations are:
Eclipse IDE for Java Developers
Version: 2019-12 (4.14.0)
Have taken latest versions for GC driver.
The same code and config is working for another laptop but not on mine. Have re-installed the application several time.
All the libraries available on internet have also been imported.
package automationFramework;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.*;
public class FirstTestCase {
public static WebDriver driver;
public static void main(String[] args) {
System.setProperty("webdriver.firefox.driver", "C:\\Users\\WC-Parul\\Downloads\\geckodriver-v0.26.0-win64.zip");
FirefoxDriver driver = new FirefoxDriver();
driver.get("https://www.google.com/");
// Launch the Online Store Website
// driver.get("http://www.shop.demoqa.com");
// Print a Log In message to the screen
System.out.println("Successfully opened the website www.Store.Demoqa.com");
// Close the driver
// driver.quit();
}
}
you say Chrome? you are not use
FirefoxDriver driver = new FirefoxDriver();
you should
WebDriver driver = new ChromeDriver();
and you should download chromedriver.exe
Hope it helps you
If not, please take a screenshot "Java Build Path"
Extract C:\\Users\\WC-Parul\\Downloads\\geckodriver-v0.26.0-win64.zip and provide geckodriver.exe path
Something like
System.setProperty("webdriver.firefox.driver", "C:\\Users\\WC-Parul\\Downloads\\geckodriver.exe");
FirefoxDriver driver = new FirefoxDriver();
driver.get("https://www.google.com/");
Make sure your downloaded correct version of geckodriver
If you want to run the code on Chrome browser then download compatible chromedriver.exe from here as per your chrome browser version and use below code:
System.setProperty("webdriver.chrome.driver", "drive_path\chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");

org.openqa.selenium.remote.UnreachableBrowserException How to define EXE path?

I have this java class code in package ChromeBrowser (That I made)
package ChromeBrowser;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class LaunchChrome{
public static void main(String[] args){
String url = "<<<The URL I want to open>>>";
WebDriver driver = setUp();
launch(driver, url);
}
static void launch(WebDriver driver, String url) {
driver.navigate().to(url);
}
static WebDriver setUp() {
System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
return driver;
}
}
But when I run it, I get the error:
Exception in thread "main" org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
Steps I've taken:
I have chrome installed.
I have downloaded the webdriver, and checked it is in C:\Selenium\chromedriver.exe
java jdk is in environment path
webdriver is in environment path, added in attempt to solve, didnt work
The code compiles and runs on my colleague's machine
Expected result:
Chrome browser opens at The URL I want to open.
Do I need to define the path to the chrome executable at C:\Program Files (x86)\Google\Chrome\Application?
Help me please thanks in advance.
EDIT:: I've tried most of the other stack overflow questions with the name of the error, but they haven't helped.
I am not really sure what the issue is but you can try below suggestions-
System.setProperty("webdriver.chrome.driver", "C:/Selenium/chromedriver.exe");
This could be a compatibility issue between the 'selenium', 'Chrome browser version' and 'chrome driver' version that you are using. If
you are using Selenium 2.53, then using chrome driver 2.25 should work
for you.
Download latest chrome driver from seleniumhq.org
Add 127.0.0.1 localhost to C:\Windows\System32\drivers\etc\hosts.
Seems like there is some issue with creating session with Firefox. Try the following code and test using Chrome browser.
You need to download the executable driver from: https://sites.google.com/a/chromium.org/chromedriver/downloads
public static void main(String[] args){
System.setProperty("webdriver.chrome.driver",
"/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
driver = new ChromeDriver();
//Puts an Implicit wait, Will wait for 10 seconds before throwing exception
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Launch website
driver.get("http://www.calculator.net/");
//Maximize the browser
driver.manage().window().maximize();
or Check the network settings (proxy, firewall, antivirus software), something is blocking
connections between selenium and the browser.

Having issues with firefox driver or chrome driver for selenium

For ex my chrome when dropped in the commpand prompt gives me the path
- /Applications/Google\ Chrome.app
I set
System.setProperty("webdriver.chrome.driver", "/Applications/Google/Chrome.app");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
But it doesnt work, same with firefox. I used a lot of suggestions already given but none seems to work. Can someone pls let me know if there is something to be added?
Why have you used "/Applications/Google/Chrome.app". You would need to provide the path of the driver only, not the browser. Below is the code for Firefox, but you would need to download and configure GeckoDriver (for latest version of FF and Selenium 3.x)
public class FirefoxTest {
#Test
public void FirefoxTest_Test1() {
System.setProperty("webdriver.gecko.driver","D:\\Firefox\\geckodriver.exe");
FirefoxDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
}
}
Check this link for complete details for downloading and setup of Geckodriver with Firefox - http://automationtestinghub.com/selenium-3-0-launch-firefox-with-geckodriver/
For Chrome: Need to Download fresh Chrome Driver from http://chromedriver.storage.googleapis.com/index.html?path=2.24/
and mention local system path up to chomedriver.exe
System.setProperty("webdriver.chrome.driver","G:\\ravik\\Ravi-Training\\Selenium\\Drivers\\cd\\chromedriver.exe");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeDriver d1 = new ChromeDriver(capabilities);
For FF: if your firefox version is latest(46.0 or above) then user geckodriver along with selenium 2.53.0 jar files. download geckodriver form https://github.com/mozilla/geckodriver/releases and then save it as "wires" in your local system. mention local system path up to wires.
System.setProperty("webdriver.gecko.driver", "G:\\ravik\\Ravi-Training\\Selenium\\Marionette for firefox\\wires.exe");
WebDriver driver = new MarionetteDriver();
Hope this could be helpful.
The easiest way to use chrome driver is.. download and place the driver into the bin folder of your project. no need to set the path of the driver location

unable to launch opera using selenium

This command didn't launch opera. thrown error "Runner threw exception on construction".
driver=new OperaDriver();
driver.get("url");
Even this didn't launch opera but thrown same error "Runner threw exception on construction".
System.setProperty("webdriver.opera.driver", "path of OperaDriver.exe");
driver=new OperaDriver();
driver.get("url");
This didn't launch opera thrown error "Could not start Opera: launcher unable to start binary".
DesiredCapabilities capabilities = DesiredCapabilities.opera(); //in this command opera is stroked.
capabilities.setCapability("opera.binary", "path of OperaDriver.exe");
driver = new OperaDriver(capabilities);
But by using the 2nd and 3rd step codes with the following path "C:\Program Files\Opera\launcher.exe", opera LAUNCHED but URL/website didn't open in the browser.
try this:
Separate OperaDriver
You can also use OperaDriver as a standalone dependency in your project. Download the package from the Github project's download section and extract it to a location of your choice. For your own projects include the lib/ directory on your classpath, for example:
javac -classpath "lib/*:." Example.java
you can also refer the selenium wiki for opera here once.
I used this and it worked.
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Devi\\Downloads\\operadriver_win32\\operadriver.exe");
driver =new ChromeDriver();
I tried this with Windows 10, Selenium 3.5.2, Opera 52.0 and OperaDriver 2.35 and the following code works for me.
DesiredCapabilities capablities=DesiredCapabilities.opera();
System.setProperty("webdriver.opera.driver", "C:\\automation\\opera\\operadriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBinary("C:\\Program Files\\Opera\\launcher.exe");
capablities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
OperaDriver driver = new OperaDriver(capablities);
driver.get("https://www.google.com");
driver.findElement(By.name("q")).sendKeys("how to use opera with selenium");

How do I setup the InternetExplorerDriver so it works

I am using WebDriver and I have downloaded the InternetExplorerDriver and was wondering what I do with it after it is downloaded?
This says to put the driver in my path. Not really certain what exactly they are talking about there.
Has anyone used this and if so could you provide detailed steps on how to set it up so it will work?
I am getting the following error:
The path to the driver executable must be set by the
webdriver.ie.driver system property
I downloaded the executables for IE and the Chrome driver. Where do I set it at?
Unpack it and place somewhere you can find it. In my example, I will assume you will place it to C:\Selenium\iexploredriver.exe
Then you have to set it up in the system. Here is the Java code pasted from my Selenium project:
File file = new File("C:/Selenium/iexploredriver.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
WebDriver driver = new InternetExplorerDriver();
Basically, you have to set this property before you initialize driver
Reference:
Driver executable must be set by the webdriver.ie.driver system property
If you are using RemoteDriver things are different. From http://element34.ca/blog/iedriverserver-webdriver-and-python :
You will need to start the server using a line like
java -jar selenium-server-standalone-2.26.0.jar -Dwebdriver.ie.driver=C:\Temp\IEDriverServer.exe
I found that if the IEDriverServer.exe was in C:\Windows\System32\ or its subfolders, it couldn't be found automatically (even though System32 was in the %PATH%) or explicitly using the -D flag.
Another way to resolve this problem is:
Let's assume:
path_to_driver_directory = C:\Work\drivers\
driver = IEDriverServer.exe
When getting messsage about path you can always add path_to_driver_directory containing driver to the PATH environment variable.
Check:
http://java.com/en/download/help/path.xml
Then simply check in cmd window if driver is available - just run cmd in any location and type name of driver.
If everything works fine then you get:
C:\Users\A>IEDriverServer.exe
Started InternetExplorerDriver server (32-bit)
2.28.0.0
Listening on port 5555
Thats it.
This is just to help somebody in future.
When we initiate InternetExplorerDriver() instance in a java project it uses IEDriver.exe (downloaded by individuals) which tries to extract temporary files in user's TEMP folder when it's not in path then ur busted.
Safest way is to provide your own extract path as shown below
System.setProperty("webdriver.ie.driver.extractpath", "F:\\Study\\");
System.setProperty("webdriver.ie.driver", "F:\\Study\\IEDriverServer.exe");
System.setProperty("webdriver.ie.logfile", "F:\\Study\\IEDriverServer.log");
InternetExplorerDriver d = new InternetExplorerDriver();
d.get("http://www.google.com");
d.quit();
public class NavigateUsingAllBrowsers {
public static void main(String[] args) {
WebDriver driverFF= new FirefoxDriver();
driverFF.navigate().to("http://www.firefox.com");
File file =new File("C:/Users/mkv/workspace/ServerDrivers/IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
WebDriver driverIE=new InternetExplorerDriver();
driverIE.navigate().to("http://www.msn.com");
// Download Chrome Driver from http://code.google.com/p/chromedriver/downloads/list
file =new File("C:/Users/mkv/workspace/ServerDrivers/ChromeDriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
WebDriver driverChrome=new ChromeDriver();
driverChrome.navigate().to("http://www.chrome.com");
}
}
Basically you need to download the IEDriverServer.exe from Selenium HQ website without executing anything just remmeber the location where you want it
and then put the code on Eclipse like this
System.setProperty("webdriver.ie.driver", "C:\\Users\\juan.torres\\Desktop\\QA stuff\\IEDriverServer_Win32_2.32.3\\IEDriverServer.exe");
WebDriver driver= new InternetExplorerDriver();
driver.navigate().to("http://www.youtube.com/");
for the path use double slash //
ok have fun !!
Here is the exact solution, which worked in my case:
On IE 7 or higher on Windows Vista or Windows 7, you must set the Protected Mode settings for each zone to be the same value. The value can be on or off, as long as it is the same for every zone. To set the Protected Mode settings, choose "Internet Options..." from the Tools menu, and click on the Security tab. For each zone, there will be a check box at the bottom of the tab labeled "Enable Protected Mode".
Additionally, "Enhanced Protected Mode" must be disabled for IE 10 and higher. This option is found in the Advanced tab of the Internet Options dialog.
System.setProperty("webdriver.ie.driver","C:\\Users\\ssin22\\Downloads\\IEDriverServer_x64_2.48.0\\IEDriverServer.exe");
package Testing;
import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class LaunchIE {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.ie.driver","C:\\Users\\ssin22\\Downloads\\IEDriverServer_x64_2.48.0\\IEDriverServer.exe");
WebDriver driver=new InternetExplorerDriver();
driver.get("http://google.com");
}
}
using System.Text;
...
..
static void Main(String[] args){
var driver = new InternetExplorerDriver(#"C:\Users\PathToTheFolderContainingIEDriver.exe");
driver.Navigate().GoToUrl("https://www.google.com/");
Console.Read();
}
You need not include the .exe file. The path to the folder containing the .exe worked for me
WebDriverManager allows to automate the management of the binary drivers (e.g. chromedriver, geckodriver, etc.) required by Selenium WebDriver.
Link: https://github.com/bonigarcia/webdrivermanager
you can use something link this: WebDriverManager.iedriver().setup();
add the following dependency for Maven:
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>x.x.x</version>
<scope>test</scope>
</dependency>
or see: https://www.toolsqa.com/selenium-webdriver/webdrivermanager/

Categories

Resources