Getting Firefox logs in c# using Selenium throwing NullReferenceException - c#

I am trying to record the log files in Firefox using Selenium in c#;
I have put together a very simple example to try and open a browser and get the logs.
However this is throwing a 'Object reference not set to an instance of an object' exception on the following line.
var entries = driver.Manage().Logs.GetLog(LogType.Browser);
Can anyone help as to why this is happening and if there is any solution?
Here is the full code
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using System;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
FirefoxOptions options = new FirefoxOptions();
options.SetLoggingPreference(LogType.Browser, LogLevel.All);
var driver = new FirefoxDriver(options);
driver.Navigate().GoToUrl("http://stackoverflow.com");
var entries = driver.Manage().Logs.GetLog(LogType.Browser);
foreach (var entry in entries)
{
Console.WriteLine(entry.ToString());
}
Console.ReadLine();
}
}
}

I believe that this is an unsupported feature, here's the referenced issues:
https://github.com/SeleniumHQ/selenium/issues/1161
https://github.com/mozilla/geckodriver/issues/284

Related

how slove this error c# selenium find element locator

in need scraping data from this web site i used C# using selenium When extracting data from the element, I run into a problem that it does not find id and classname and Xpath Knowing that I have extracted the parameters of the element
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
namespace Selenium_Automation
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver = new ChromeDriver("chromedriver.exe");
// This will open up the URL
driver.Url = "https://www.olx.com.eg/";
//driver.FindElement(By.Name("q")).SendKeys("PHP");
//driver.FindElement(By.ClassName("gLFyf")).SendKeys("mysql");
driver.FindElement(By.XPath("//*[#id=\"body-wrapper\"]/div/header/div/div[4]/div/button/span")).Click();
driver.FindElement(By.XPath("//*[#id=\"strat-strat-dialog-545-container\"]/div/div/div/div/div[2]/div[2]/button[4]")).Click();
}
}
}

C# Automation Edge Browser - using Edge Driver - auto testing program - Failure: No matching capabilities found (SessionNotCreated)

Greetings stackoverflow community,
I am trying to compile and run the programcode from this website:
https://social.msdn.microsoft.com/Forums/en-US/7bdafd2a-be91-4f4f-a33d-6bea2f889e09/c-sample-for-automating-ms-edge-chromium-browser-using-edge-web-driver
I followed all the instructions listed in the link and set my paths were I wanted them.
The program and the edge driver starts running, but then an error appears.
"An error exeption "System.InvalidOperationException" appeared in WebDriver.dll.
Further Inforamtion: session not created: No matching capabilities found (SessionNotCreated)"
This is the code from my program, more or less copied from the link above:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var anaheimService = ChromeDriverService.CreateDefaultService(#"C:\edgedriver_win64", "msedgedriver.exe");
// user need to pass the driver path here....
var anaheimOptions = new ChromeOptions
{
// user need to pass the location of new edge app here....
BinaryLocation = #"
C: \Program Files(x86)\Microsoft\Edge\Application\msedge.exe "
};
IWebDriver driver = new ChromeDriver(anaheimService, anaheimOptions); -- error appears at this line
driver.Navigate().GoToUrl("https: //google.com/");
Console.WriteLine(driver.Title.ToString());
driver.Close();
}
}
}
I would really appreciate your help!
Best Regards
Max
The article you refer to is a bit out of date. Now we don't need to use ChromeDriver to automate Edge. You can refer to the official doc about how to use WebDriver to automate Microsoft Edge.
I recommend using Selenium 4. Here I install Selenium 4.1.0 NuGet package and the sample C# code is like below:
using System;
using OpenQA.Selenium.Edge;
namespace WebDriverTest
{
class Program
{
static void Main(string[] args)
{
var options = new EdgeOptions();
options.BinaryLocation = #"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
var driver = new EdgeDriver(#"C:\edgedriver_win64", options);
driver.Navigate().GoToUrl("https://www.google.com");
Console.WriteLine(driver.Title.ToString());
driver.Close();
}
}
}

C# SELENIUM: Can't access URL after setting custom FIREFOX profile

I am trying to connect to profile, its successfully connect to custom firefox profile, but the problem after that is command FirefoxDriver driver = new FirefoxDriver(options); no more works, works only if i remove options then no custom profile.
the before last line returns error OpenQA.Selenium.WebDriverException: 'Process unexpectedly closed with status 0' or The HTTP request to the remote WebDriver timed out after 60 seconds, it only works if I remove options inside FirefoxDriver: FirefoxDriver driver = new FirefoxDriver(options);
Also, doing options.AddArgument("-profile" + "C:\Users\Chill\AppData\Roaming\Mozilla\Firefox\Profiles\5k2mdm2k.myprofile"); instead of spliting the 2 arguments does not launch firefox in the right profile.
Or even options.AddArgument("no-sandbox") or options.AddArgument("-no-sandbox") or options.AddArgument("--no-sandbox") doesn't works, also --profile instead of -profile does not open the right profile also, here is my code anyway:
using System;
using OpenQA.Selenium; // nuget package name: Selenium.WebDriver
using OpenQA.Selenium.Firefox; // nuget package name: Selenium.WebDriver.GeckoDriver
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
FirefoxOptions options = new FirefoxOptions();
options.AddArgument("-profile");
options.AddArgument(#"C:\Users\Chill\AppData\Roaming\Mozilla\Firefox\Profiles\5k2mdm2k.myprofile"); /* type about:profiles in firefox bar to create and manage firefox profiles, from there you will see which profile used, make sure to not use the default one and use root directory */
FirefoxDriver driver = new FirefoxDriver(options); /* code stops here and puts error after closing browser or waiting until it close itself after 60 sec */
driver.Navigate().GoToUrl("https://www.google.com/"); /* can only reach this part of code if i remove turn FirefoxDriver(options); to FirefoxDriver(); on the line upper, but no more custom profile so */
}
}
}
Hope you can help im blocked on this step for 3 days
Using these nugget versions:
<PackageReference Include="Selenium.Firefox.WebDriver" Version="0.27.0" />
<PackageReference Include="Selenium.WebDriver" Version="4.0.0-beta4" />
This works for me:
using OpenQA.Selenium.Firefox;
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var options = new FirefoxOptions();
var profile = new FirefoxProfile(#"C:\Users\CatalinR\AppData\Roaming\Mozilla\Firefox\Profiles\f9n067l1.default");
options.Profile = profile;
var driver = new FirefoxDriver(options);
driver.Navigate().GoToUrl("https://www.google.com/");
}
}
}

IE in Private Mode using Selenium C#

I want to open IE in Private mode to run the set of test cases. The browser is not opening. It shows error as
The HTTP request to the remote WebDriver server for URL {URL} timed out after 60 seconds
Sample code:
InternetExplorerOptions options = new InternetExplorerOptions()
{
ForceCreateProcessApi = true,
BrowserCommandLineArguments = "-private",
};
IWebDriver driver = new InternetExplorerDriver("C:\\Reports", options);
driver.Navigate().GoToUrl("https://www.google.com");
Also I have changed the TabProcGrowth as 0 in Registry Editor.
How to open IE in private mode to run the test case? Anything I want to update in my code. Thanks in advance.
This is how I manage to launch it:
Set the TabProcGrowth as 0 in Registry Editor.
Get the Selenium.WebDriver.IEDriver64 nugget instead of the normal 32 and build the project
Get the IEDriverServer64.exe from bin\Debug\netcoreapp3.1 (the output folder where this file is generated depends on your TargetFramework: .netcore or .netstandard)
Rename that file into IEDriverServer.exe and put it somewhere in a folder
Create the driver instance using the path to that folder. In my case, I created a folder in the project and pointed there
Project: Solution Explorer View
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using System.IO;
namespace InternetExplorerPrivate
{
public class Tests
{
public IWebDriver driver;
[SetUp]
public void Setup()
{
InternetExplorerOptions options = new InternetExplorerOptions();
options.ForceCreateProcessApi = true;
options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
options.BrowserCommandLineArguments = "-private";
driver = new InternetExplorerDriver(Path.GetFullPath(#"..\..\..\IEDriver"), options);
}
[Test]
public void Test1()
{
driver.Navigate().GoToUrl("https://stackoverflow.com/");
}
}
}

Select element issue - webdriver - c#

I'm getting the below error while running in NUnit,
I'm finding an element and storing it in a variable, and while trying to select the element, i'm getting the error. Tried using in this way
IWebElement fromitem = WebDriver.FindElement(By.Id("from")); but same error persists.
Is there any way i can select the element?
Note : I verified the element id with firebug, there seems to be no problem with it.
The code below,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.Events;
using OpenQA.Selenium.Support.PageObjects;
namespace SeleniumTests
{
[TestFixture]
public class Sel
{
public static IWebDriver WebDriver;
private String baseURL;
[SetUp]
public void SetupTest()
{
WebDriver = new FirefoxDriver();
baseURL = "http://www.x-rates.com/calculator.html";
}
[TearDown]
public void TeardownTest()
{
WebDriver.Quit();
}
[Test]
public void newtest()
{
WebDriver.Navigate().GoToUrl(baseURL + "/");
var fromitem = WebDriver.FindElement(By.Id("from"));
var toitem = WebDriver.FindElement(By.Id("to"));
var fromval = new SelectElement(fromitem); //Error occurs
var toval = new SelectElement(toitem);
fromval.SelectByText("USD - US Dollar");
toval.SelectByText("INR - Indian Rupee");
WebDriver.FindElement(By.LinkText("Currency Calculator")).Click();
var curval = WebDriver.FindElement(By.CssSelector("span.ccOutputRslt")).GetAttribute("Value");
var expectedValue = 61.456825;
Thread.Sleep(900);
Assert.AreEqual(expectedValue, curval.Trim());
}
}
}
The SelectElement class can only be used with actual HTML <select> elements. In the page you provided, the element in question is an <input> element, with functionality added through CSS and JavaScript to enable it to act like a drop-down list. As such, attempting to use it with the SelectElement class will throw an exception indicating that the element is not of the correct type.
The "File does not exist" error message is a red herring. It's only there because NUnit is trying to show you the line of source code where the exception is thrown, which is a part of the WebDriver source code. The exception thrown by that line of code should be displayed somewhere within NUnit, which should contain the appropriate informational message.

Categories