selenium c# edge browser : 'Unexpected error. Unknown error' - selenium-webdriver

OS Build Number:- 18363.900
Edge Version: 83.0.478.54
MicrosoftWebDriver Version: 83.0.478.54
Here is the code:
System.Environment.SetEnvironmentVariable("webdriver.edge.driver", "E:\\edgedriver_win64\\msedgedriver.exe");
IWebDriver driver = new EdgeDriver();
I am getting error as: OpenQA.Selenium.WebDriverException: 'Unexpected error. Unknown error'

Which version of Selenium WebDriver are you using? I suggest you to use the latest version 4.0.0-alpha05 and use the code below:
EdgeOptions edgeOptions = new EdgeOptions();
edgeOptions.UseChromium = true;
edgeOptions.BinaryLocation = #"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
var msedgedriverDir = #"D:\webdriver83.0.478.54";
var driver = new EdgeDriver(msedgedriverDir, edgeOptions);
driver.Navigate().GoToUrl("https://bing.com");
Thread.Sleep(3000);
driver.Close();
Note: Change the paths in the code to your owns.
Result:

Related

FirefoxDriver Cannot start the driver service

Have an app that has been running without issue (or so Ive been told) for months, but as of a few weeks ago started failing. Looking at the logs, I see a lot of these errors
EXCEPTION: Error intializing Firefox driver. Error message: 'Cannot
start the driver service on http://localhost:52022/';
WebDriverException stack: 'OpenQA.Selenium.WebDriverException: Cannot
start the driver service on http://localhost:52022/ at
OpenQA.Selenium.DriverService.Start() at
OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command
commandToExecute) at
OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String
driverCommandToExecute, Dictionary`2 parameters) at
OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities
desiredCapabilities) at
OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor
commandExecutor, ICapabilities desiredCapabilities) at ... ... .
The project is a console app that is using the Selenium project to try to connect to an external website but it seems it is having trouble just starting up. The code where this exception is caught is below.
this._logger = logger;
this._processLog = new List<string>();
FirefoxProfile profile = null;
FirefoxOptions options = null;
profile = new FirefoxProfile();
profile.SetPreference("browser.download.folderList", 2);
profile.SetPreference("browser.download.dir", downloadPath);
profile.SetPreference("browser.download.manager.alertOnEXEOpen", false);
profile.SetPreference("browser.download.manager.focusWhenStarting", false);
profile.SetPreference("browser.download.manager.closeWhenDone", true);
profile.SetPreference("browser.download.manager.showAlertOnComplete", false);
profile.SetPreference("browser.helperApps.alwaysAsk.force", false);
profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/json,text/csv");
profile.SetPreference("browser.download.manager.useWindow", false);
profile.SetPreference("browser.download.panel.shown", false);
options = new FirefoxOptions();
options.AddArguments("-headless");
options.AddArgument("no-sandbox");
options.Profile = profile;
try
{
this._driver = new FirefoxDriver(driverPath, options, TimeSpan.FromSeconds(180));
}
catch (WebDriverException ex)
{
string message = string.Format("Error intializing Firefox driver. Error message: '{0}'; WebDriverException stack: '{1}' ",
ex.Message, ex.ToString());
result.ProcessLog.Add(new LogMessage(LogLevel.EXCEPTION, message));
throw ex;
}
catch(Exception ex)
{
string message = string.Format("Error intializing Firefox driver. Error message: '{0}'; Exception stack: '{1}' ",
ex.Message, ex.ToString());
result.ProcessLog.Add(new LogMessage(LogLevel.EXCEPTION, message));
throw ex;
}
The computer where this process runs has the latest version of Firefox installed, however, the FireFox Selenium driver looks dated. Could there be a dependency issue?

Setting downloads directory for chrome driver suddenly stopped working after upgrade

After I've upgraded my project to use latest java selenium webdriver 3.7.1 and latest chrome driver 2.33 I noticed that my tests dealing with Excel export started to fail.
After debugging I see that for some reasons downloads directory is not being set to custom location and instead the default one is used. Nothing shows up in logs or whatsoever.
Any help appreciated.
Setting downloads directory is like usual:
val prefs = Map(
"profile.default_content_settings.popups" -> Integer.valueOf(0),
"download.default_directory" -> downloadsDir,
"credentials_enable_service" -> false,
"profile.password_manager_enabled" -> false
)
val chromeOptions:ChromeOptions = new ChromeOptions()
chromeOptions.setExperimentalOption("prefs", prefs)
val d = new ChromeDriver(chromeOptions)
I use WebDriver 3.7.1 and Chromedriver 2.33 and of course Chrome v.62 (try check Your Version of Chrome), and I can download files.
I'm using folder
public static String javaIoTmpDir = System.getProperty("java.io.tmpdir")+"/autotest/";
and I create/clean it with script in TestNG annotation: #BeforeSuite
File theDir = new File(javaIoTmpDir);
// if the directory does not exist, create it
if (!theDir.exists()) {
System.out.println("creating directory: " + theDir.getName());
boolean result = false;
try{
theDir.mkdir();
result = true;
}
catch(SecurityException se){
se.printStackTrace();
}
if(result) {
System.out.println(javaIoTmpDir+" created");
}
}
FileUtils.cleanDirectory(new File(javaIoTmpDir));
}

Getting error "java.lang.IllegalStateException:The driver executable does not exist"

I have imported my selenium code from windows to mac and installed chromedriver for mac and updated systemproperty accordingly.but it is throwing exception and showing previously set path:
java.lang.IllegalStateException: The driver executable does not exist: /Users/Preet12/Desktop/Selenium/AddaStore/C:\Users\cp\Downloads\chromedriver_win32\chromedriver.exe
however the path in mac is : /Users/Preet12/Desktop/Selenium/AddaStore/chromedriver 2
it is showing path of window also i.e. C:\Users\cp\Downloads\chromedriver_win32\chromedriver.exe
Code:
public class WithTestNG
{
WebDriver driver;
#BeforeClass
public void StartBrowser()
{
System.setProperty("webdriver.chrome.driver","/Users/Preet12/Desktop/Selenium/AddaStore/chromedriver 2" );
driver = new ChromeDriver();
driver.manage().window().maximize();
}
Try Navigating to the folder containing chromedriver and run
"chmod +x chromedriver"

Exception in thread "main" java.lang.Error:Unresolved compilation problems: working with selenium

I am a begineer in selenium and trying my own after installation of eclipse, java, selenium, TestNg and started writing the below code.
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
WebDriver cannot be resolved to a type
FirefoxDriver cannot be resolved to a type
at Mypackage.MyClass.main(MyClass.java:8)
package Mypackage;
public class MyClass {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
String baseUrl = "http://google.com";
String expectedTitle = "Welcome: Hello";
String actualTitle = "";
driver.get(baseUrl);
actualTitle = driver.getTitle();
if (actualTitle.contentEquals(expectedTitle)) {
System.out.println("Test Passed!");
} else {
System.out.println("Test Failed");
}
driver.close();
}
}
you need to add selenium webdriver jar files to the project
right click on project-->goto build path-->configure build path-->click on "Add external jars"-->add selenium jar files from your local machine-->click ok-->now mouseover on WebDriver in your code-->click "import webdriver"--now run your code-->you will get rid of the exception.

PhantomJSDriver- How to initialize the driver?

Can someone tell me how I begin to test with PhantomJS? I have downloaded the PhantomJS exe file and also set my "path" variable.
And I do something similar for chrome like
if(config.getProperty("browser").equals("chrome"))
{
System.setProperty("webdriver.chrome.driver", "C:drivers\\chromedriver.exe");
driver= new ChromeDriver();
}
which is
if(config.getProperty("browser").equals("phantom"))
{
System.setProperty("webdriver.chrome.driver", "C:\\drivers\\chromedriver.exe");
driver= new PhantomJSDriver();
}
But, this does not work. Please help. I do not want to use RemoteWebDriver or GhostDriver
I figured it out:
if(config.getProperty("browser").equals("phantom"))
{
System.setProperty("phantomjs.binary.path", "C:\\drivers\\chromedriver.exe");
driver= new PhantomJSDriver();
}
PhantomJSDriver accepts path to executable as a constructor argument:
if (config.getProperty("browser").equals("phantom"))
driver= new PhantomJSDriver("C:\\drivers\\chromedriver.exe");
Find the phantomjs code below and set the property key and value:
//System.setProperty("phantomjs.binary.path","phantomjs.exe path");
System.setProperty("phantomjs.binary.path", System.getProperty("user.dir")+"/ExternalLibraryFiles/phantomjs-2.1.1-windows/phantomjs-2.1.1-windows/bin/phantomjs.exe");
driver =new PhantomJSDriver();

Resources