简体   繁体   中英

How to open chrome in Selenium webdriver?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM