How to use Phantomjs for opening a website in Selenium - selenium

I'm trying to use headless webkit of PhantomJs for opening google.com through selenium webdriver but when I execute the code system throws an error. phantomJs.exe is placed in E directory. Please help me resolve this issue.
public static void main(String[] args) throws Exception {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "E:\\phantomjs.exe");
WebDriver driver = new PhantomJSDriver();
driver.get("http://www.google.com");
}
Error:
Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the phantomjs.binary.path capability/system property/PATH variable; for more information, see https://github.com/ariya/phantomjs/wiki. The latest version can be downloaded from http://phantomjs.org/download.html
at com.google.common.base.Preconditions.checkState(Preconditions.java:197)
at org.openqa.selenium.phantomjs.PhantomJSDriverService.findPhantomJS(PhantomJSDriverService.java:236)
at org.openqa.selenium.phantomjs.PhantomJSDriverService.createDefaultService(PhantomJSDriverService.java:181)
at org.openqa.selenium.phantomjs.PhantomJSDriver.(PhantomJSDriver.java:104)
at org.openqa.selenium.phantomjs.PhantomJSDriver.(PhantomJSDriver.java:94)
at multidrivers.main(multidrivers.java:35)

Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the phantomjs.binary.path capability/system property/PATH variable;
The above issue is due to the driver not being initialized with the DesiredCapabilities object:
WebDriver driver = new PhantomJSDriver();
Updating the code as below should solve your issue:
WebDriver driver = new PhantomJSDriver(caps);
Let me know if you have any queries.

Related

Getting "The path to the driver executable must be set by the webdriver.chrome.driver system property"though set correct path

My code is very simple
code:
WebDriver wd =new ChromeDriver();
System.setProperty("webdriver.chrome.driver",
"D:\\List_of_Jar\\chromedriver.exe");
String baseUrl = "https://www.google.com";wd.get(baseUrl);
have downloaded and added jar as "Java-3.4.0" from selenium hq site.
Download Google Chrome Driver-2.29 from the same website and located it in "D:\List_of_Jar" path.
When I run the above code I getting an error as
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:738)
Getting version error though did proper configuration. so kindly help me for fixing the issue.
Details:
OS: Windows XP.
Java : JDK1.8 and JRE1.8.
Selenium : version 3.4
Driver path should be set before browser launch as given below.
System.setProperty("webdriver.chrome.driver","D:\List_of_Jar\chromedriver.exe");
WebDriver wd =new ChromeDriver();
String baseUrl = "https://www.google.com";
wd.get(baseUrl);"
You are setting chrome driver path incorrectly. Property must be set before WebDriver initialization.
Set property like this -
System.setProperty("webdriver.chrome.driver","D:\\List_of_Jar\\chromedriver.exe")
WebDriver wd =new ChromeDriver();
String baseUrl = "https://www.google.com";
wd.get(baseUrl);"
If you are using IntelliJ IDE, then on IntelliJ without setting up within the 'Run > Edit configurations > VM Options' i will just meet this error:
Failed scenarios:
C:/Users/DATestAdmin/IdeaProjects/TestLogin/src/test/resources/login.feature:4 # Scenario: Successfully logging in
1 Scenarios (1 failed)
3 Steps (3 skipped)
0m0.194s
java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property;
So once i've added the path to my chromedriver locally in 'Run > Edit configurations > VM Options':
-Dwebdriver.chrome.driver="C:\\Users\\This\\Is\\Where\\ChromeDriverIs\\chromedriver_win32.exe"
I'm now able to launch my Chrome browser successfully.
I totally agree with Murthi, but better is to set relative path to the driver, NOT the absolute.
Relative path looks like:
System.setProperty("webdriver.chrome.driver", "src/test/resources/drivers/chromedriver.exe");
Abosulte: is the path to the driver in your PC.
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
Why?
It is a good practice to have driver inside your project, not just in your computer. Just find or create folder f.e. resources, inside resources create folder called f.e. drivers and import your driver/drivers exe files there.
The below lines works fine
public class TestNGFile {
public String baseUrl = "https://google.com";
String driverPath = "C:\\\\Users\\\\Documents\\\\selenium\\\\chromedriver_win32\\\\chromedriver.exe";
#Test
public void verifyHomepageTitle() {
System.out.println("launching chrome browser");
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Documents\\selenium\\chromedriver_win32\\chromedriver.exe");
//System.setProperty("webdriver.gecko.driver", driverPath);
WebDriver driver = new ChromeDriver();
driver.get(baseUrl);
String expectedTitle = "Google";
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle);
driver.close();
Try:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Demo2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "I:\\Bhasker-ShiroCode\\work\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://google.com");
}
}
To avoid Error:
webdriver.chrome.driver ( should be in small letters )
have to give correct chromedriver.exe ( correct path )
Import all Selenium jars under class Path
I was getting the same error, since chrome driver was not installed on my machine.
Install the chrome driver. Follow:
https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver
You should use Chocolatey as the Selenium wiki dictates. It will work straight away.
Windows users with Chocolatey installed: choco install chromedriver
https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver
I also encountered the same problem. Following fix, made my application run smoothly.
Firstly, the required version of chrome driver could be found from below link.
http://chromedriver.storage.googleapis.com/index.html
It is best to use always the latest version. After downloading, set the path of chrome driver in System.setProperty("webdriver.chrome.driver","{Your path Chrome Driver}");
Follow the code fragment.
System.out.println("Creating Chrome Driver");
// Set Chrome Driver
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("{Your URL}");
System.out.println("Wait a bit for the page to render");
TimeUnit.SECONDS.sleep(5);
System.out.println("Taking Screenshot");
File outputFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String imageDetails = "D:\\Images";
File screenShot = new File(imageDetails).getAbsoluteFile();
FileUtils.copyFile(outputFile, screenShot);
System.out.println("Screenshot saved: {}" + imageDetails);
I faced the same issue.
"The path to the driver executable must be set by the webdriver.chrome.driver system property."
downloaded the driver and have set in system property.
https://www.youtube.com/watch?v=Ny_8ikCbmcQ

Exception in thread "main" java.lang.NullPointerException using WebDriver Driver=new Chrome() and maximize Chrome browser window using selenium script

How to maximize Chrome browser window using selenium script?
This is my code:
package newpackage;
import org.openqa.selenium.WebDriver;
public class MyClass {
public static void main(String[] args) {
WebDriver Driver=new Chrome();
Driver.get("http://www.google.com");
Driver.manage().window().maximize();
}
}
However, I get this error:
Exception in thread "main" java.lang.NullPointerException
at newpackage.MyClass.main(MyClass.java:10)
To work with Selenium 3.4.0 you need to download the latest chromedriver 2.29 from here and update your Google Chrome to latest release of 58.x. Save the chromedriver in your system and provide the absolute path in your code through System.setProperty as below.
Now, the constructor for initializing ChromeDriver and Chrome Browser is as follows:
WebDriver driver = new ChromeDriver();
WebDriver driver = new ChromeDriver(options);
Note: The method is ChromeDriver() but not Chrome() which have caused java.lang.NullPointerException
Finally, to maximize Chrome browser window using selenium script you need to take help of ChromeOptions class as follows:
public static void main(String[] args)
{
System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.navigate().to("https://google.com");
//do your actions
driver.quit();
}
In the script you have written the Driver object is null. Try instantiate the Driver properly by using Chromedriver.
System.setProperty("webdriver.chrome.driver", "c:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
Driver.get("http://www.google.com");
Driver.manage().window().maximize();
var options = new ChromeOptions();
options.AddArguments("disable-infobars");
options.AddArguments("--start-maximized");
options.AddArguments("--disable-extensions");
var chromeDriver = new ChromeDriver(options);
use driver.manage().window().fullscreen(); instead
It should work.

Webdriver exception selenium grid

Not able to launch test after setting all , here is code :
public void browserSetup() throws MalformedURLException
{
DesiredCapabilities capability =DesiredCapabilities.firefox();
driver=new RemoteWebDriver(new URL(nodeurl),capability);
capability.setBrowserName("firefox");
capability.setPlatform(Platform.VISTA);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(PageUrlReff.HomePageUrl);
}
Getting following error:
org.openqa.selenium.WebDriverException: The path to the driver executable must be set by the webdriver.gecko.driver system property
On the machine that is running your Selenium node please download the binaries for
ChromeDriver
GeckoDriver
IEDriverServer (Assuming your node is a WINDOWS box, if not, then you don't need this)
and make the location of these binaries available as part of your PATH environment variable. After that your automation should run fine and you wouldn't be seeing this error message.

Exception on Selenium Grid program

Have started the hub and registered the node. Then wrote a program in eclipse:
after running the program i am getting below exception:-
Exception in thread "main" java. lang.Class Cast Exception: java.lang.String cannot be cast to java.util.Map.
the remote web driver is :
Remote Web Driver driver = new Remote Web Driver(client URL,capability);
I've had the same error. The problem was in class org.openqa.selenium.remote.RemoteWebDriver (75):
Map<String, Object> rawCapabilities = (Map<String, Object>) response.getValue();
It's happened because I've given the wrong URL parameter for RemoteWebDriver():
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/"), capability);
That's why response had a String type.
Please check your url. It should be:
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);
I hope it will help you :)

WebDriver not opening URL

I'm very new to selenium so I'm having trouble spotting the problem with my code. I'm using a webDriver backed selenium object, it starts the driver but never opens the URL and the driver just closes after a few moments. The last time this happened to me it was just because I had left "http" out of the URL. So what's causing it this time?
public void testImages() throws Exception {
Selenium selenium = new WebDriverBackedSelenium(driver, "http://www.testsite.com/login");
System.out.println(selenium.getXpathCount("//img"));
}
The setup looks like:
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\User1\\Desktop\\chromedriver_win_16.0.902.0\\chromedriver.exe");
driver = new ChromeDriver();
Thread.sleep(2000);
}
The teardown method just consists of driver.close().
I'm using selenium 2.14 and the testNG Eclipse plug-in.
You might need to do the following
selenium.open("www.testsite.com/login");
Check out this example from the selenium site:
// You may use any WebDriver implementation. Firefox is used here as an example
WebDriver driver = new FirefoxDriver();
// A "base url", used by selenium to resolve relative URLs
String baseUrl = "http://www.google.com";
// Create the Selenium implementation
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
// Perform actions with selenium
selenium.open("http://www.google.com");
selenium.type("name=q", "cheese");
selenium.click("name=btnG");
// Get the underlying WebDriver implementation back. This will refer to the
// same WebDriver instance as the "driver" variable above.
WebDriver driverInstance = ((WebDriverBackedSelenium) selenium).getUnderlyingWebDriver();
//Finally, close the browser. Call stop on the WebDriverBackedSelenium instance
//instead of calling driver.quit(). Otherwise, the JVM will continue running after
//the browser has been closed.
selenium.stop();
link to selenium
You would need to add driver.get(url) like below.
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\User1\\Desktop\\chromedriver_win_16.0.902.0\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://www.testsite.com/login");
}