Unable to instantiate Marionette driver with both a FirefoxProfile & FirefoxDriverService - c#

I am currently updating my C# solution with the new Marionette driver for Firefox.
I have managed to get the driver to successfully launch a url with the following code
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(Directory.GetCurrentDirectory(),"wires-0.6.0-win.exe");
service.FirefoxBinaryPath = #"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
FirefoxDriver driver = new FirefoxDriver(service);
driver.Navigate().GoToUrl("http://www.google.com");
However i need to add a profile to the driver at initialisation in order to set the proxy. I was previously doing this like the following (for older Firefox versions).
private static IWebDriver InitialiseFirefoxDriver(Proxy proxy)
{
FirefoxProfile profile = new FirefoxProfile();
if (proxy != null)
{
profile.SetProxyPreferences(proxy);
}
return new FirefoxDriver(profile);
}
Unfortunately the FirefoxDriver constructor only allows me to pass in either a FirefoxDriverService or a FirefoxProfile. Does anybody know of a way that i can manage to give the driver both sets of config information before creating the driver (or even after)?
Thanks.

This is example code use FirefoxProfile & FirefoxDriverService. I want hide CommandPromptWindow and Mute.
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true;
FirefoxProfile _Profile = new FirefoxProfile();
_Profile.SetPreference("media.volume_scale", "0.0");
FirefoxOptions option = new FirefoxOptions();
option.Profile = _Profile;
var driver = new FirefoxDriver(service,option,TimeSpan.FromSeconds(20));
driver.Navigate().GoToUrl("https://www.youtube.com/watch?v=hxiitzCKRek");

Related

Crawlera/Zyte proxy authentication using C# and Selenium

I've tried a number of ways of using Zyte (formally Crawerla) proxies with Selenium.
They provide
1- API key (username)
2- Proxy url/port.
No password is needed.
What I have tried...
ChromeOptions options = new ChromeOptions();
var proxy = new Proxy();
proxy.Kind = ProxyKind.Manual;
proxy.IsAutoDetect = false;
proxy.SocksUserName = "<<API KEY>>";
proxy.SocksPassword = "";
proxy.HttpProxy =
proxy.SslProxy = "proxy.crawlera.com:8011";
options.Proxy = proxy;
IWebDriver driver = new ChromeDriver(options);
Which, when selenium loads produces this:
Funnily enough, if I manually add the username (API Key) it will indeed load, but this defeats the purpose of automation.
The second method I tried was:
ChromeOptions options = new ChromeOptions();
options.AddArguments("--proxy-server=<API KEY>::proxy.crawlera.com:8011");
options.AddArguments("--log-level=OFF");
IWebDriver driver = new ChromeDriver(options);
I used :: as the password is blank.
And the error to this method is:
[46784:44492:0219/015119.757:ERROR:validation_errors.cc(87)] Invalid
message: VALIDATION_ERROR_DESERIALIZATION_FAILED
I guess Zyte/Crawerla knowledge isn't really needed, it's more how to provide selenium with a username, but no password, and have it use the proxy successfully.
Does anybody have any idea? (examples appreciated)
you can use this Crawlera-Headless-Proxy https://github.com/zytedata/zyte-smartproxy-headless-proxy when using a headless browser.
This will act as a MITM proxy and will handle the Authentication of SPM as well.
So you will not need to mention the Crawlera/SmartProxyManager Authentication within your C# Selenium Script.
Crawlera-headless-proxy can be used as a standalone binary like this
crawlera-headless-proxy -c config.toml
You can mention the API Key, port number, and other configs in the config.toml file.
It will start the MITM server at the localhost:3128. In your C# you need to mention the proxy host/port should be localhost/127.0.0.1 and port as 3128.
The requests from your C# headless script needs to be sent to Crawlera-Headless-Proxy and then Crawlera-Headless-Proxy will send the request to the SmartProxyManager/Crawlera.
So your code may look like this for ex:
ChromeOptions options = new ChromeOptions();
var proxy = new Proxy();
proxy.Kind = ProxyKind.Manual;
proxy.IsAutoDetect = false;
proxy.HttpProxy =
proxy.SslProxy = "127.0.0.1:3128";
options.Proxy = proxy;
IWebDriver driver = new ChromeDriver(options);

Selenium Firefox not adding extensions

I'm trying to get ublock installed on Firefox with Selenium. The problem is, the extension won't load. The browser loads fine but does not add the extension.
FirefoxProfile profile = new FirefoxProfile();
profile.AddExtension("ublock_origin-1.17.4-an+fx.xpi");
FirefoxOptions options = new FirefoxOptions
{
Profile = profile
};
IWebDriver driver = new FirefoxDriver(options);
I've seen this but I don't want to use an old version of firefox, and i haven't seen any other fix for it.
Currently using v0.23.0 of geckodriver for win64.
How can I add extensions to firefox with selenium? (without using an old version)
Try splitting out the options part, see if that makes a difference:
FirefoxProfile profile = new FirefoxProfile();
profile.AddExtension(*file path*);
FirefoxOptions options = new FirefoxOptions();
options.Profile = profile;
IWebDriver driver = new FirefoxDriver(options);
FirefoxProfile profile = new FirefoxProfile();
FirefoxOptions options = new FirefoxOptions
{
Profile = profile
};
IWebDriver driver = new FirefoxDriver(options);
firefoxDriver.InstallAddOnFromFile("ublock_origin-1.17.4-an+fx.xpi"); // Dear Wizard, this is the magic
Use Selenium 4.X and use the FirefoxDriver.InstallAddOnFromFile method instead of the the FirefoxOptions.AddExtension

How to start ChromeDriver in headless mode

I want to try out headless chrome, but I am running into this issue, that I can't start the driver in headless mode. I was following google documentation. am I missing something ? The code execution gets stuck in var browser = new ChromeDriver(); line
Here is my code:
var chromeOptions = new ChromeOptions
{
BinaryLocation = #"C:\Users\2-as Aukstas\Documents\Visual Studio 2017\Projects\ChromeTest\ChromeTest\bin\Debug\chromedriver.exe",
DebuggerAddress = "localhost:9222"
};
chromeOptions.AddArguments(new List<string>() {"headless", "disable-gpu" });
var browser = new ChromeDriver(chromeOptions);
browser.Navigate().GoToUrl("https://stackoverflow.com/");
Console.WriteLine(browser.FindElement(By.CssSelector("#h-top-questions")).Text);
UPDATE
Chrome version 60 is out so all you need to do is to download Chromdriver and Selenium via Nuget and use this simple code and everything works like a charm. Amazing.
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
...
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments("headless");
using (var browser = new ChromeDriver(chromeOptions))
{
// add your code here
}
DATED
There is a solution until the official release of Chrome 60 will be released. You can download Chrome Canary and use headless with it. After installation set BinaryLocation to point to chrome canary also comment out the DebuggerAddress line(it forces chrome to timeout):
var chromeOptions = new ChromeOptions
{
BinaryLocation = #"C:\Users\2-as Aukstas\AppData\Local\Google\Chrome SxS\Application\chrome.exe",
//DebuggerAddress = "127.0.0.1:9222"
};
chromeOptions.AddArguments(new List<string>() { "no-sandbox", "headless", "disable-gpu" });
var _driver = new ChromeDriver(chromeOptions);
For you that did not get reference for ChromeDriver.
Use this step :
Download the dll from this: http://seleniumtestings.com/selenium-download/
Extract, and you should see: Selenium.WebDriverBackedSelenium.dll, ThoughtWorks.Selenium.Core.dll, WebDriver.dll and WebDriver.Support.dll
Add those files via "Add Reference"
Now you can use it:
String url = "http://www.google.com";
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments(new List<string>() {
"--silent-launch",
"--no-startup-window",
"no-sandbox",
"headless",});
var chromeDriverService = ChromeDriverService.CreateDefaultService();
chromeDriverService.HideCommandPromptWindow = true; // This is to hidden the console.
ChromeDriver driver = new ChromeDriver(chromeDriverService, chromeOptions);
driver.Navigate().GoToUrl(url);
====
If after you run, you are still facing error about no ChromeDriver.exe file, try to add the Selenium.WebDriver.ChromeDriver, WebDriver.ChromeDriver, WebDriver.ChromeDriver.win32, Selenium.Chrome.WebDriver via nuget.
As alternative:
Add 2 libraries via NuGet like below picture.
Try below Code:
String url = "http://www.google.com";
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments(new List<string>() { "headless" });
var chromeDriverService = ChromeDriverService.CreateDefaultService();
ChromeDriver driver = new ChromeDriver(chromeDriverService, chromeOptions);
driver.Navigate().GoToUrl(url);
What OS you're running? I see on developers.google.com/web/updates/2017/04/headless-chrome that headless won't be available on Windows until Chrome 60.
Below i have given how to set the headless to true for firefox and chrome browsers.
FirefoxOptions ffopt = new FirefoxOptions();
FirefoxOptions option = ffopt.setHeadless(true);
WebDriver driver = new FirefoxDriver(option);
ChromeOptions coptions = new ChromeOptions();
ChromeOptions options = coptions.setHeadless(true);
WebDriver driver = new ChromeDriver(options);

WebDriver Portable Firefox c#

I tried to load Portable Firefox
FirefoxProfile profile = new FirefoxProfile();
var binary = new FirefoxBinary(Directory.GetCurrentDirectory()+ #"\FirefoxPortable64\FirefoxPortable.exe");
var driver = new FirefoxDriver(binary, profile);
But i get error Unable to find a matching set of capabilities in line var driver = new FirefoxDriver(binary, profile);
can u help me pls ?
And in line var driver = new FirefoxDriver(binary, profile); i have warning:
FirefoxDriver should not be constructed with a FirefoxBinary object. Use FirefoxOptions instead. This constructor will be removed in a future release.
how i can rewrite the code ?
UPD:
I rewrite code:
FirefoxProfile profile = new FirefoxProfile();
FirefoxOptions options = new FirefoxOptions();
options.Profile = profile;
options.BrowserExecutableLocation = Directory.GetCurrentDirectory() + #"\FirefoxPortable64\FirefoxPortable.exe";
var driver = new FirefoxDriver( options);
But it doesnt work, again have error Unable to find a matching set of capabilities
Solution ;
options.BrowserExecutableLocation = Directory.GetCurrentDirectory() + #"\FirefoxPortable32\App\Firefox\firefox.exe";

Selenium Firefox Profile Path not taken into account

I just want to test locally. With Internet Explorer it works. With Firefox, I get a timeout on line driver.FindElement :
var capabilitiesInternet = new OpenQA.Selenium.Remote.DesiredCapabilities();
capabilitiesInternet.SetCapability("ignoreProtectedModeSettings", true);
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl(url);
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement category = wait.Until<IWebElement>((d) =>
{
return d.FindElement(By.Name("login"));
});
// Login
driver.FindElement(By.Name("login")).SendKeys("test");
Error message is httpRequest to remotedriver timeout.
Upate: I think it's due to the fact that I have a portable version of Firefox 21 and an older version of FF which cannot work with Selenium whereas Selenium launch old version. So I tried to indicate the path of portable :
var capabilitiesInternet = new OpenQA.Selenium.Remote.DesiredCapabilities();
capabilitiesInternet.SetCapability("ignoreProtectedModeSettings", true);
string path = #"C:\Portable";
FirefoxProfile ffprofile = new FirefoxProfile(path);
IWebDriver driver = new FirefoxDriver(ffprofile);
Unfortunately it keeps running the old version (I cannot change the old version because of corporate environment).
Is there anyway to make this profile work ?
Not sure if it is your problem, but in order to 'point' Selenium to where Firefox is located, you are looking for the FirefoxBinary class:
var binary = new FirefoxBinary("pathtofirefox");
string path = #"C:\Portable";
FirefoxProfile ffprofile = new FirefoxProfile(path);
var driver = new FirefoxDriver(binary, profile);

Categories

Resources