RemoteWebDriver unable to execute Javascript - c#

I'm trying to use Selenium RemoteWebDriver (in C#) with Multiloginapp.
Here is my code for initiating a driver:
int mlaClientPort = 35000;
Uri driverUrl = new Uri("http://127.0.0.1:" + mlaClientPort + "/api/v1/webdriver");
// set desired capabilities to connect to a pre-defined browser profile
DesiredCapabilities dc = new DesiredCapabilities();
dc.SetCapability("multiloginapp-profileId", profileId);
RemoteWebDriver driver = new RemoteWebDriver(driverUrl, dc);
It starts up the browser fine (FireFox).
However, when I attempt to execute Javascript like below:
var jsEx = (IJavaScriptExecutor)driver;
object result = jsEx.ExecuteScript("alert('test');");
I get the error:
'Underlying driver instance does not support executing javascript
This is my first time using RemoteWebDriver but as far as I see, javascript is working in the browser when using it from the console etc.
What could be the issue here?

Related

How to solve 2 different Facebook accounts login chrome driver with Selenium C# (windows form)?

I have 2 different Facebook accounts need to be login (I am trying to learn selenium). Somebody helps me to use the loop of for or the commands of array with listviewtextbox (listviewtextbox contains the Facebookemail and Facebookpass to login). Thanks for all!!
Chromedriver driver, driver1;
driver = new chrome driver(services, options); //services and options all created
driver1 = new chrome driver(services, options);
driver.Url = "https://facebook.com/login";
driver1.Url = "https://facebook.com/login";
driver.Navigate();
driver1.Navigate();
public void Login()
{
driver.FindElenment(By.ID("email")).SendKeys("aaa");
driver1.FindElenment(By.ID("email")).SendKeys("bbb");
driver.FindElenment(By.ID("pass")).SendKeys("aaa");
driver1.FindElenment(By.ID("pass")).SendKeys("bbb");
driver.FindElenment(By.Name("login")).Click();
driver1.FindElenment(By.Name("login")).Click();
}

Your connection is not secure - using Selenium.WebDriver v.3.6.0 + Firefox v.56

I'm writing tests with Selenium + C# and I face an important issue because I didn't found solution when I test my site with secure connection (HTTPS). All solutions I found on stackoverflow are out of date or doesn't work.
I tried to exercise all solutions from below question:
Selenium Why setting acceptuntrustedcertificates to true for firefox driver doesn't work?
But they did not help me solve the problem
Nor is it the solution of using Nightly FireFox.
Still, when the selenium loading Firfox browser, I see the page: "Your connection is not secure".
Configuration:
Firefox v56.0
Selenium.Firefox.WebDriver v0.19.0
Selenium.WebDriver v3.6.0
my code is:
FirefoxOptions options = new FirefoxOptions();
FirefoxProfile profile = new FirefoxProfile();
profile.AcceptUntrustedCertificates = true;
profile.AssumeUntrustedCertificateIssuer = false;
options.Profile = profile;
driver = new FirefoxDriver(FirefoxDriverService.CreateDefaultService() , options , TimeSpan.FromSeconds(5));
Drivers.Add(Browsers.Firefox.ToString() , driver);
Thank for your help!
Updates to my question here:
Note 1: To anyone who has marked my question as a duplicate of this question:
Firefox selenium webdriver gives “Insecure Connection”
I thought that it is same issue, but I need solution for C#, I try match your JAVA code to my above code
First, I changed to TRUE the below statment:
profile.AssumeUntrustedCertificateIssuer = true;
second, I create new FF profile ("AutomationTestsProfile")
and try to use it:
Try 1:
FirefoxProfile profile = new FirefoxProfileManager().GetProfile("AutomationTestsProfile");
try 2:
FirefoxProfile profile = new FirefoxProfile("AutomationTestsProfile");
I Run 2 options, but still the issue exists.
Note 2: I attached screenshot of my problem, it appears when the driver try to enter text to user-name on login page.
I noticed that when I open my site with FF, Firefox displays a lock icon with red strike-through red strikethrough icon in the address bar,
but near the username textbox not appears the msg:
"This connection is not secure. Logins entered here could be compromised. Learn More" (as you writed on the duplicate question),
So maybe there is a different problem?
You are setting the properties on the profile. The FirefoxOptions has a property AcceptInsecureCertificates, set that to true.
Forget the profile, this is what you want:
var op = new FirefoxOptions
{
AcceptInsecureCertificates = true
};
Instance = new FirefoxDriver(op);
For me, the profile setting AcceptUntrustedCertificates was not enough, I also had to set option security.cert_pinning.enforcement_level. My startup looks like
// no idea why FirefoxWebDriver needs this, but it will throw without
// https://stackoverflow.com/questions/56802715/firefoxwebdriver-no-data-is-available-for-encoding-437
CodePagesEncodingProvider.Instance.GetEncoding(437);
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var service = FirefoxDriverService.CreateDefaultService(Environment.CurrentDirectory);
service.FirefoxBinaryPath = Config.GetConfigurationString("FirefoxBinaryPath"); // path in appsettings
var options = new FirefoxOptions();
options.SetPreference("security.cert_pinning.enforcement_level", 0);
options.SetPreference("security.enterprise_roots.enabled", true);
var profile = new FirefoxProfile()
{
AcceptUntrustedCertificates = true,
AssumeUntrustedCertificateIssuer = false,
};
options.Profile = profile;
var driver = new FirefoxDriver(service, options);
It works for me for following settings (same as above):
My env:
win 7
firefox 61.0.2 (64-bit)
Selenium C# webdriver : 3.14.0
geckodriver-v0.21.0-win32.zip
==============================
FirefoxOptions options = new FirefoxOptions();
options.BrowserExecutableLocation = #"C:\Program Files\Mozilla Firefox\firefox.exe";
options.AcceptInsecureCertificates = true;
new FirefoxDriver(RelativePath,options);

Selenium can't handle multiple ChromiumWebBrowser instances in C#

I have two instances of the ChromiumWebBrowser in my WinForms project (Visual Studio 2012). My goal is to have the second browser instance "copy" the behavior of the user input in the first browser instance. I can successfully retrieve the input from the first browser, and I managed to hook up Selenium in the project as well.
However, I'm having one issue. Whenever Selenium sends its commands, the first browser is the one that responds to them. For the life of me, I can't seem to figure out how to make the second browser respond. Whenever I completely remove the first browser, the second one starts responding correctly, but adding the first browser again will make only have the first browser use the Selenium commands. I even tried to switch out the moments the browsers are added to the form, but to no avail: whenever there are two available, the wrong one is responsive.
Relevant code:
public BrowserManager(Controller controller, string startingUrl)
{
_controller = controller;
var settings = new CefSettings { RemoteDebuggingPort = 9515 };
Cef.Initialize(settings);
// Input browser
inputBrowser = new ChromiumWebBrowser(startingUrl);
var obj = new XPathHelper(this);
inputBrowser.RegisterJsObject("bound", obj); //Standard object registration
inputBrowser.FrameLoadEnd += obj.OnFrameLoadEnd;
// Output browser
var browserSettings = new BrowserSettings();
var requestContextSettings = new RequestContextSettings { CachePath = "" };
var requestContext = new RequestContext(requestContextSettings);
outputBrowser = new ChromiumWebBrowser(startingUrl);
outputBrowser.RequestContext = requestContext;
outputBrowser.AddressChanged += InitializeOutputBrowser;
outputBrowser.Enabled = false;
outputBrowser.Name = "outputBrowser";
}
The selenium part:
public class SeleniumHelper
{
public SeleniumHelper()
{
DoWorkAsync();
}
private Task DoWorkAsync()
{
Task.Run(() =>
{
string chromeDriverDir = #"ActionRecorder\bin\x64\Debug\Drivers";
var chromeDriverService = ChromeDriverService.CreateDefaultService(chromeDriverDir);
chromeDriverService.HideCommandPromptWindow = true;
ChromeOptions options = new ChromeOptions();
options.BinaryLocation = #"ActionRecorder\bin\x64\Debug\ActionRecorder.exe";
options.DebuggerAddress = "127.0.0.1:9515";
options.AddArguments("--enable-logging");
using (IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver(chromeDriverService, options))
{
driver.Navigate().GoToUrl("http://www.google.com");
var query = driver.FindElement(By.Name("q"));
query.SendKeys("A google search test");
query.Submit();
}
});
return null;
}
}
And finally, a screenshot for some visualization:
Some help with the issue would be very much appreciated. If i missed some crucial info, feel free to ask for it. Thanks in advance!
Greetz,
Tybs
The behavior is correct. You have one debug address and you can only have one debug address for CEF. Which means when you use Selenium it is only seeing one browser.
By default Selenium will send an command to current active Tab or Window. Now in your case you have multiple Chrome view embedded, but they are technically Chrome Tab/Windows which you have placed on the same form.
So if you are in luck below code in should be able to move you to the Window you are interested in
driver.SwitchTo().Window(driver.WindowHandles.Last());
See if it works. If it doesn't then your only other workaround would be to change the order of Adding ChromiumWebBrowser and that should reverse the window it works on.
Below are some important threads that you should read from top to bottom. Very relevant to your issue/request
https://code.google.com/archive/p/chromiumembedded/issues/421
https://github.com/cefsharp/CefSharp/issues/1076

How to make two instance of selenium safaridriver?

How can i make two instance of selenium driver ?? i used these codes and i am having a problem.
SafariDriver sf = new SafariDriver();
sf.Navigate().GoToUrl("http://marathonbet.com/");
SafariDriver sf2 = new SafariDriver();
sf2.Navigate().GoToUrl("http://bet365.com/");
here is the screenshot: http://sdrv.ms/1fDzQYT
you can instantiate 2 instance of selenium webdriver, use the code below.
WebDriver sf1 = new SafariDriver();
sf1.Navigate().GoToUrl("http://marathonbet.com/");
WebDriver sf2 = new SafariDriver();
sf2.Navigate().GoToUrl("http://bet365.com/");
You need to instantiate WebDriver object with SafariDriver, that you were not doing correctly.

How to get the Browser info in C# WebDriver?

I see a ICapabilities interface to get the Browser info;Did couple of googling with no luck for any code example; Can anybody please share anything how I can get the browser info for a particular IWebDriver instance ? I am using C# webdriver.
In order to get info defined in ICapabilities interface, you need to cast IWebDriver instance to RemoteWebDriver. Then you can get the info about BrowserName, IsJavaScriptEnabled, Platform and Version.
IWebDriver driver = new FirefoxDriver();
ICapabilities capabilities = ((RemoteWebDriver)driver).Capabilities;
// then you have
// capabilities.BrowserName;
// capabilities.IsJavaScriptEnabled;
// capabilities.Platform;
// capabilities.Version;
Based on the old Yi Zeng answer, I was able to acces with the next code:
IWebDriver driver = new FirefoxDriver();
ICapabilities capabilities = ((WebDriver)driver).Capabilities;
// then you have
// capabilities.GetCapability("browserName");
// ...
I use below code to get Chrome driver version
IWebDriver driver = new ChromeDriver();
ICapabilities capabilities = ((OpenQA.Selenium.WebDriver)driver).Capabilities;
var SeleniumWebDriverName = driver.GetType().ToString();
var SeleniumWebDriverVersion = (capabilities.GetCapability("chrome") as Dictionary<string, object>)["chromedriverVersion"];
Console.WriteLine( "DRIVER NAME ====" + SeleniumWebDriverName );
Console.WriteLine( "VERSION ====" + SeleniumWebDriverVersion + Environment.NewLine);
I've stumbled across an easier way if you just need to know which driver is running to get around a hack:
Driver.GetType().ToString();

Categories