How Do I implement PhantomJS + Wedriver on c# - selenium-webdriver

I have written the following code and downloaded PhantomJS intot he specified folder on c:\ but am getting the error: "Unable to connect to remote server ...". Here is the code I wrote:
[TestMethod]
public void HeadlessBrowser()
{
IWebDriver driver = new PhantomJSDriver("C:\\trashStuff\\phantomjs-1.9.0-windows"); //or some other driver
driver.Navigate().GoToUrl("http://yahoo.com");
// Lets take a screenshot to really make sure we did visit the site above
Console.WriteLine("Take A screen shot");
Screenshot myScreenShot = ((ITakesScreenshot)driver).GetScreenshot();
myScreenShot.SaveAsFile("c:\\trashStuff\\screenshot.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
// after the run, go to the location above and find screenshot.jpg
}

Download PhantomJS or add it to your project using NuGet Package Manager. I got it using NuGet and didn't have to put the path to the driver as one of the parameters in PhantomJSDriver class.
var driver = new PhantomJSDriver();

Related

IE driver 4.0.0 stuck at This is the initial start page for the WebDriver server

I am using Internet Explorer driver 4.0.0(32bit) and selenium updated version 4.1.2when I run the code using Internet Explorer driver 4.0.0 it is stuck at This is the initial start page for the WebDriver server.
even though I check the setting in Internet Explorer:
All the protected mode is disabled
zoom setting is 100% for Internet Explorer and windows also
enchanted protected mode is off
but when I change the Internet Explorer driver to 3.1.4 it works fine. I also tried the Internet Explorer 4.0.0 for 64 but the same issue I am facing please help me.
public class EdgeTest {
static RemoteWebDriver driver = null;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.ie.driver",
"D:\\Automation\\Jar Details\\Drivers\\IEDriverServer_Win32_4.0.0\\IEDriverServer.exe");
InternetExplorerOptions ieOptions = new InternetExplorerOptions();
ieOptions.attachToEdgeChrome();
ieOptions.withEdgeExecutablePath("C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe");
ieOptions.setCapability("ignoreProtectedModeSettings", true);
ieOptions.setCapability("ignoreZoomSettings",true);
System.out.println("this:"+ieOptions.getCapability("ignoreZoomSettings"));
driver = new InternetExplorerDriver(ieOptions);
InternetExplorerDriverService.createDefaultService();
driver.get("https://github.com/");
WebElement elem = driver.findElement(By.name("q"));
elem.sendKeys("gitu");
elem.sendKeys(Keys.ENTER);
driver.quit();
error: "Could not create a new remote session" this is an error
getting
It looks like you are using selenium webdriver to automate Edge IE mode. I tried executing your code and I found this code to work fine (using IE driver 4.0.0 and Selenium 4.1.2).
But when executing code again before the code has not been executed complete, you get the same problem you describe (execute code twice at once). like this:
So I think when the IE driver may be occupied, you can try to open the task manager, end the related task (IE driver 32 bit), and then re-execute the code, I think this should be useful to you.

"The driver is not executable" when I try to launch a chrome browser with Selenium [duplicate]

This question already has answers here:
Mac OSX - IllegalStateException: The driver is not executable:
(12 answers)
Closed 3 years ago.
I am getting this error.
Exception in thread "main" java.lang.IllegalStateException: The driver
is not executable: /home/hashconnect/Desktop/Priya
bugsheet/selenium/chromedriver.exe
Creating a new folder "Drivers" in that project and changed the location as ".\drivers\chromedriver.exe"
This is my code:
package demo;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class test {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","/home/hashconnect/Desktop/Priya bugsheet/selenium/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
Thread.sleep(5000);
}
}
Expected: System should launch the chrome browser
Actual:
It showing error message:
Exception in thread "main" java.lang.IllegalStateException: The driver
is not executable: /home/hashconnect/Desktop/Priya
bugsheet/selenium/chromedriver.exe
Go to task manager, close all instance of chrome browser by end task and it should work.
One reason could be that the path to the driver executable has got a space in it. A suggestion would be to move the driver to a different path and update the path in your code and retry.
Try doing a
"ls -ltr /home/hashconnect/Desktop/Priya bugsheet/selenium/chromedriver.exe"
Or it could be that there might be multiple instances of ChromeDriver already running and they are preventing the spawning of more instances. So the suggestion is to kill all those instances from the TaskManager on Windows or with kill -9 on linux.
The Problem is Path which you have given.
Try giving full path
Example: C:/Users//Desktop/...../chromedriver.exe
This will Solve your Problem.
Dry Ran it:
java.lang.IllegalStateException: The driver executable does not exist: C:\main\resources\chromedriver.exe
The Above Error occurs if you give invalid path (non relative path)
It Works:
C:\Users\SA\test\src\resources\chromedriver.exe
The driver is not executable : this issue might occur if the chromedriver.exe does not have execution permission
I am not sure how selenium works on Ubuntu. Still, the Set up you have downloaded seems to be for a windows version(.exe). It doesn't work on Linux.
Please download the chrome driver for Linux from the below link
Download Chrome Driver for Linux
Reference:
Reference link

Unable to connect a Run A Script in Selenium using IE Driver

Trying to Run a Script in Luna Eclipse using IE Driver
and getting 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. Here is the code trying to Run:
package com.newpack;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class SecondClass {
public static void main(String[] args)
{
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability("requireWindowFocus", true);
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
capabilities.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL,"https://www.google.co.in");
System.setProperty("webdriver.ie.driver","M:\\workspace\\IEDriver.exe");
WebDriver driver ;
driver = new InternetExplorerDriver(capabilities);
String baseUrl = "https://www.google.co.in";
driver.get(baseUrl);
String expectedTitle = "Google";
String actualTitle = "";
actualTitle = driver.getTitle();
if (actualTitle.contentEquals(expectedTitle)){
System.out.println("Test Passed!");
} else {
System.out.println("Test Failed");
}
driver.close();
System.exit(0);
}
}
try once following steps
Got to
internet options>>Security>>
click on:"Reset all zones to default level"
ensure that the 'Enable protected mode' checkbox is ticked for all the four zones. Like i.e Internet, Local internet, Trusted Sites, Restricted Sites.
Internet Explorer needs to be configured first in order to successfully run a selenium script.
Make sure you complete all the following steps:
Go to Internet Options > Security and enable "Protected Mode" for all the 4 zones: Internet, Local internet, Trusted Sites, Restricted Sites.
Also, click on Custom Level for all 4 zones and reset Recent Custom Settings to same level, say "Medium" or "High".
If this does not work, please update your IEDriverServer from Selenium Official Downloads

Launch chrome browser

I am trying to launch chrome browser (version 26.0) using webdriver.
I am getting the following error message.
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 http://code.google.com/p/selenium/wiki/ChromeDriver.
at com.google.common.base.Preconditions.checkState(Preconditions.java:176)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:105)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:69)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:107)
at googleSearch.main(googleSearch.java:13)
Code I used:
driver = new ChromeDriver();
driver.navigate().to("http://www.google.com/");
I use mac 10.8.2.
For this to work, you need to:
Install Chrome
Install Chrome Web Driver
Make sure you have the chrome web driver in you path, for example on Windows something pointing to chromedriver2_win32_0.8. You can put that in your path by either: (a) Modifying your windows path environment variable, or; (b) adding the following to your java command line options:
-Dwebdriver.chrome.driver='/path/to/driver'
In case of using selenium grid
-Dwebdriver.chrome.driver='/path/to/driver'
has to be added while creating a node from command line.
1) In case of using selenium without GRID:
System.setProperty("webdriver.chrome.driver","/absolute/path/to/chromedriver");
driver = new ChromeDriver();
does the job.
2) In case of using selenium with GRID:
System.setProperty("webdriver.chrome.driver","/absolute/path/to/chromedriver");
driver = new ChromeDriver();
And from command line, while creating a node for chrome browser one needs to pass
-Dwebdriver.chrome.driver='/absolute/path/to/chromedriver'
The above two changes did the job for me, apart from this I was getting this libnss3.so not found error which I solved by creating a symlink of libnss3.so present in /usr/lib/x86_64-linux-gnu/ folder to /usr/lib/
ln -s /usr/lib/x86_64-linux-gnu/libnss3.so /usr/lib/libnss3.so
PS: Also make sure that you are using 64bit OR 32bit version of chrome driver as per your system.
For chrome to work with selenium-webdriver you need to have not only a working chrome browser installed, but also the chromedriver executable. Note that these are TWO different executable files that both need to be specified.
change the permission of file and then run your code again.
Open command prompt and navigate to directory where your chrome exe exists and write
chmod 777 filename
Hope it will solve your problem.

HTTP Status 500 - Java Runtime Environment (JRE) version 1.7 is not supported by this driver

I am trying to access MS SQL server 2005 from a servlet file. I am using JDBC 4.0 driver.
I have already added the JAR files sqljdbc.jar and sqljdbc4.jar files to my Tomcat /lib folder.
But while running code I am getting an error
HTTP Status 500 - Java Runtime Environment (JRE) version 1.7 is not supported by this driver. Use the sqljdbc4.jar class library, which provides support for JDBC 4.0.
How is this caused and how can I solve it?
My code is:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection conn = conn = DriverManager.getConnection("jdbc:sqlserver://localhost;databaseName=school;user=sa;password=123");
PrintWriter pwOut = res.getWriter();
pwOut.println("Connected");
Statement st = conn.createStatement();
String searchCriteria = req.getParameter("txtSearch");
ResultSet rs = st.executeQuery("select * from student");
res.setContentType("text/html");
The error message is pretty clear. Tomcat is using the wrong driver.
You state that you copied sqljdbc.jar and sqljdbc4.jar into the Tomcat lib folder. That is most probably the reason for your problem.
You only need sqljdbc4.jar otherwise Tomcat picks up the wrong one.
Try to delete sqljdbc.jar from the Tomcat lib folder
Here is my Code to connect java to Microsoft sql Server 2012
You only need sqljdbc4.jar that avail on offical Microsoft website. Here is the link:
http://download.microsoft.com/download/0/2/A/02AAE597-3865-456C-AE7F-613F99F850A8/sqljdbc_4.0.2206.100_enu.exe
It contains 2 jar files, and I am trying to use sqljdbc4.jar. This is the code I am using to connect:
package com.Sql.ConnectDB;
import java.sql.*;
public class DbClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
**String url="jdbc:sqlserver://localhost;databaseName=Student";**//important
String user="username";
String pass="password";
**Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");**//important
Connection con=DriverManager.getConnection(url,user,pass);
System.out.println("Conneccted Successfully");
}catch(Exception e){
e.printStackTrace();
}
}
}

Resources