Open Chrome with selenium - java

public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:/Users/myPC/Desktop/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
}
So I downloaded chromedriver.exe and tried opening Chrome using the following code, but every time I run this code I get the following error:
Error:java: package com.google.common.collect does not exist
And also whenever I try and run chromedriver.exe from desktop, chrome does not want to open.
Thanks in advance

public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:/Users/myPC/Desktop/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.navigate().to("http://www.google.com");
}

Related

The driver executable must exist

import org.openqa.selenium.WebDriver;
import org.openqa selenium.chrome.ChromeDriver;
public class SeliniumTest {
public static void main(String [] args) {
System.setProperty("webdriver.chrome.driver", chromedriver.exe");
WebDriver driver = new Chrome driver();
driver.get("https://www.google.com");
enter code here
}
}
When I run this, I get an illegal Exception:
the driver executable must exist: c:\TestProject\drivers\chromedriver.exe
I tried multiple times to install the IDE and download the right version of drivers on both Mac and Windows, but I get the same error. Can someone please help?

updated correct chrome driver path/driver version and add selenium jar too, but still getting below issue , could you help me for this?

code :
package Demo1;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Chrome {
public static void main(String[] args) {
WebDriver driver= new ChromeDriver();
System.setProperty("webdriver.chrome.driver","C:\\New folder\\chromedriver.exe");
driver.get("https://www.youtube.com/watch?v=BtmeQOcdIKI");
System.out.println(driver.getTitle());
}
}
error :
Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html
at com.google.common.base.Preconditions.checkState(Preconditions.java:847)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:134)
at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:35)
at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:159)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:355)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:94)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123)
at Demo1.Chrome.main(Chrome.java:9)
You are setting the system property too late.
Looking at the stacktrace, the exception is being thrown while executing the following line of your code:
WebDriver driver= new ChromeDriver();
At that point, the line of your code that sets the system property hasn't been reached yet.
Evidently, you need to set the system property before creating the ChromeDriver object.
The first line in your main method should be :
System.setProperty("webdriver.chrome.driver","C:\\New folder\\chromedriver.exe");
something like this.
public class Chrome {
WebDriver driver = null;
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\New folder\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://www.youtube.com/watch?v=BtmeQOcdIKI");
System.out.println(driver.getTitle());
}
}
should get the job done.

testNG problem runing a suite with open browser

I have 2 similar classes.
I changed the opening of the browser to ChromeOptions so the tests can't be execute when a browser is open.
When I am execute the testNG.xml (with the 2 tests) its open immiditly both of the browsers and the suite can't be run.
What should I need to change in order to execute this well ?
Thanks alot
public class Demo2 {
public static WebDriver driver;
public static void main(String[] args)
{
initializeDriver();
print2();
}
#BeforeTest
private static void initializeDriver() {
System.setProperty("webdriver.chrome.driver","C:\\Program Files\\Selenium\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:/Users/עמית/AppData/Local/Google/Chrome/User Data");
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);
}
#Test
public static void print2()
{
driver.get("https://www.google.com");
System.out.println("1");
driver.close();
}
}
I think this path C:/Users/עמית/AppData/Local/Google/Chrome/User Data") should use alphabet letters. Even if you try to use this, please config your font type to use the popular font as UTF-8.

Fail to run tests in Chrome headless mode

I am trying to run tests in Chrome headless mode but getting java.lang.NullPointerException
Chrome version: Version 72.0.3626.121 (Official Build) (64-bit)
Selenium version: 3.8.1
Chromedriver version: 2.45.615355
Here is my BaseTest:
public abstract class BaseTest {
public WebDriver driver;
protected abstract String getUrl();
#Before
public void setUp() {
Log.startLog("Test is Starting...");
System.setProperty("webdriver.chrome.driver", "src//test//resources//chromedriver");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setHeadless(true);
WebDriver driver = new ChromeDriver(chromeOptions);
driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get(getUrl());
}
#After
public void tearDown() {
Log.endLog("Test is Ending...");
driver.manage().deleteAllCookies();
driver.close();
}
}
When I'm running tests, not in headless mode every test works good but in headless mode, I can't even run a simple test to understand if the headless mode is working or not.
Test example:
#Test
public void test() {
System.out.println(driver.getTitle());
}
Example URL: https://www.wikipedia.org/
UPDATE:
I've created new sample project with this code:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class test {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "/Users/alexsomov/Desktop/chromedriver2");
//Set Chrome Headless mode as TRUE
ChromeOptions options = new ChromeOptions();
options.setHeadless(true);
//Instantiate Web Driver
WebDriver driver = new ChromeDriver(options);
driver.get("https://www.google.com/");
System.out.println("Page title is - " + driver.getTitle());
driver.close();
}
And bingo, everything works well... Need to figure out why code above from real project doesn't work seems something wrong with BaseTest class and when I run code with debugger I'm getting driver == null, maybe anyone have a solution how I can solve this problem :/
ANSWER
The solution was super easy, just need to change 1 string in setUp() method in BaseTest class.
This one:
WebDriver driver = new ChromeDriver(chromeOptions);
change to this:
driver = new ChromeDriver(chromeOptions);
and everything will work.
If you are using linux environment, may be you have to add --no-sandbox as well and also specific window size settings. --no-sandbox is no needed in windows if you set user container properly.
disable-gpu Only on Windows. Other platforms no longer require it. The --disable-gpu flag is a temporary work around for a few bugs.
if(browser.equalsIgnoreCase("HLChrome")){
//Headless chrome browser and configure
WebDriverManager.chromedriver().setup();
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--no-sandbox");
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("disable-gpu");
// chromeOptions.addArguments("window-size=1400,2100"); // linux should be activate
driver = new ChromeDriver(chromeOptions);
ANSWER The solution was super easy, just need to change 1 string in setUp() method in BaseTest class.
This one:
WebDriver driver = new ChromeDriver(chromeOptions);
change to this:
driver = new ChromeDriver(chromeOptions);
and everything will work.

java Eclipse selenium webdriver throws exception

So I was just writing a simple test program to open google in firefox. And it's not working.
Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
at com.google.common.base.Preconditions.checkState(Preconditions.java:738)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:111)
at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:38)
at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:112)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:302)
at org.openqa.selenium.firefox.FirefoxDriver.createCommandExecutor(FirefoxDriver.java:312)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:272)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:267)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:263)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:122)
at Test.main(Test.java:7)
My code:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Test {
public static void main(String args[]) {
WebDriver driver = new FirefoxDriver();
System.out.println("Hello Google...");
driver.get("http://google.com");
}
}
Please download Geckodriver from below mentioned link
https://github.com/mozilla/geckodriver/releases.
In Your Test program set Geckodriver path-
public class links {
public static void main(String[] args) throws InterruptedException {
WebDriver driver;
//System.setProperty("webdriver.firefox.marionette", "D:\\vidya\\selenium\\geckodriver-v0.13.0-win64\\geckodriver.exe");
driver =new FirefoxDriver();
driver.manage().window().maximize();

Categories