Why chromedriver redownload the adblock extension on all runs? - c#

I try to load ChromeDriver with adblock, and somehow it reloads downloading the extension everytime it runs and shows this message:
If you see this message every time you start AdBlock, please make sure you are not using a file cleaner that also cleans 'localStorage' files.
var options = new ChromeOptions();
options.AddArgument("--no-experiments");
options.AddArgument("--disable-translate");
options.AddArgument("--disable-plugins");
options.AddArgument("--no-default-browser-check");
options.AddArgument("--clear-token-service");
options.AddArgument("--disable-default-apps");
options.AddArgument("--no-displaying-insecure-content");
options.AddArgument("--disable-bundled-ppapi-flash");
options.AddExtension(#"D:\AdBlock-v2.6.5\adblock.crx");
using (IWebDriver driver = new ChromeDriver(options))
{
driver.Navigate().GoToUrl(url);
}

Try to use the same chrome profile on every run. This must resolve the issue.
Code to do this located here: Load Chrome Profile using Selenium WebDriver

Related

C# Selenium Webdriver

I started using selenium with CS and have one issue. When code is compiled, program cannot find webdriver path, because it's being moved into the .exe file. I fixed this problem, by copying driver into the bin folder, so program can access it again. However, I want it to be able to access that driver inside .exe file.
I was doing this in python using os path:
def resource_path(relative_path: str) -> str:
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.dirname(__file__)
return os.path.join(base_path, relative_path)
If anyone knows how to do this in cs, please let me know.
Code that I'm using in c#:
var browser = new EdgeDriver();
browser.Navigate().GoToUrl(link);
webdrivermanager should be more helpful here. you can add its Nuget and use to manage drivers for browsers without requiring the driver exe files.
I use something like this and call this method everytime I need a browser.
public static InternetExplorerDriver InitBrowser(string browserName)
{
switch (browserName)
{
case "IE":
{
var IE_DRIVER_PATH = #"C:\PathTo\IEDriverServer";
InternetExplorerDriver driver = new InternetExplorerDriver(IE_DRIVER_PATH);
return driver;
}
}
return null;
}
This allows you to define the path from which to grab the driver, and so you wont have to depend on it being in your BIN folder. There are other solutions but this is what I have that works really well for me. You are set up to use this method for other browsers by adding more switch cases, and also from here you can easily add your browser options. You can call the method in your tests using:
InternetExplorerDriver driver = InitBrowser(IE);
Here it is simplified without the switch case:
var IE_DRIVER_PATH = #"C:\PathTo\IEDriverServer";
InternetExplorerDriver driver = new InternetExplorerDriver(IE_DRIVER_PATH);

Firefox opening wrong profile with selenium c#

I'm using selenium 3.14 with geckodriver 0.24, I'm using following code to run the existing profiles I have already created for my different accounts.
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.Proxy = pro; //my proxy object
firefoxOptions.AddArgument("-profile " + path); //path to the profile
FirefoxDriverService ffDriverService = FirefoxDriverService.CreateDefaultService();
ffDriverService.BrowserCommunicationPort = 2828;
PropertiesCollection.Driver = new FirefoxDriver(ffDriverService, firefoxOptions);
I have multiple profiles each with a different proxy. Right now, the browser is started and everything works very well for the first profile, but once I dispose the browser and start a new one with new profile and proxy, the driver opens the same last browser. I've tried many solutions and have changed selenium to old versions but no luck.
One thing I noticed in the console is that when driver opens the browser, it runs a command on console like this:
1561625708285 mozrunner::runner INFO Running command: "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe" "-marionette" "-profile C:\\Users\\Usr\\Desktop\\fprofiles\\pf1" "-foreground" "-no-remote"
if I run this command from cmd the profile issue remains there:
"C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe" "-marionette" "-profile C:\\Users\\Usr\\Desktop\\fprofiles\\pf1" "-foreground" "-no-remote"
If I remove the " from command and make it complete text it will look like this
"C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe" -marionette -profile C:\\Users\\Usr\\Desktop\\fprofiles\\pf1 -foreground -no-remote
I cloned the selenium project of OpenQA and tried to debug there but that also uses geckodriver.exe and I guess geckodriver.exe is responsible for getting arguments and passing to firefox.
Last but the least option will be to compile geckodriver(which has been developed in RUST) once again as per my consent but the programming language is RUST and that's going to be a long long job for achieving what I need.
Has anyone faced the same problem? How can I get it fixed?
Try loading browser profile based on it's name. An example with profile called 'selenium_profile':
public static WebDriver driver;
public static String driverPath = "C:\\Users\\pburgr\\Desktop\\selenium-tests\\FF_driver_0_23\\geckodriver.exe";
public static WebDriver startFF() {
FirefoxOptions options = new FirefoxOptions();
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile selenium_profile = allProfiles.getProfile("selenium_profile");
options.setProfile(selenium_profile);
options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
System.setProperty("webdriver.gecko.driver", driverPath);
driver = new FirefoxDriver(options);
driver.manage().window().maximize();
return driver;
}
It must not be static so you can parse name of desired profile in argument.

Headless Browser C# and Alternatives

currently I have the following code using selenium and phantomjs in c#:
public class Driver
{
static void Main()
{
using (var driver = new PhantomJSDriver())
{
driver.Navigate().GoToUrl("https://www.website.com/");
driver.Navigate().GoToUrl("https://www.website.com/productpage/");
driver.ExecuteScript("document.getElementById('pdp_selectedSize').value = '10.0'"); //FindElementById("pdp_selectedSize").SendKeys("10.0");
driver.ExecuteScript("document.getElementById('product_form').submit()");
driver.Navigate().GoToUrl("http://www.website/cart/");
Screenshot sh = driver.GetScreenshot();
sh.SaveAsFile(#"C:\temp\test.jpg", ImageFormat.Png);
}
}
}
My objective is to be able to add a product to my cart and then checkout automatically. The screenshot is just included to test whether the code was successfully working. My first issue is that I often get an error that it cannot find the element with product id "pdp_selectedSize". I'm assuming this is because the the webdriver hasn't loaded the page yet, so I'm looking for a way to keep checking until it finds it without having to set a specific timeout.
I'm also looking for faster alternatives to use instead of a headless browser. I used a headless browser instead of http requests because I need certain cookies to be able to checkout on the page, and these cookies are set through javascript within the page. If anyone has a reccommendation for a faster method, it would be greatly appreciated, thanks!
For your first question, it would behoove you to look into using ExpectedConditions' which is part of theWebDriverWaitclass inSelenium`. The following code sample was taken from here and only serves as a reference point.
using (IWebDriver driver = new FirefoxDriver())
{
driver.Url = "http://somedomain/url_that_delays_loading";
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement myDynamicElement = wait.Until<IWebElement>(d =>
d.FindElement(By.Id("someDynamicElement")));
}
More on WebDriverWaits here.
As to your second question, that is a really subjective thing, in my opinion. Headless Browsers aren't necessarily any faster or slower than a real browser. See this article.

How to solve "Document was unloaded during execution (UnexpectedJavaScriptError)"

I'm trying to update our Selenium tests to work with the latest Firefox. This code snipet shows how I initialize the Driver. Instance is a class member: NgWebDriver Instance
FirefoxOptions ffOptions = new FirefoxOptions();
ffOptions.SetPreference("marionette", true);
IWebDriver NonProtractorInstance = new FirefoxDriver(ffOptions);
Instance = new NgWebDriver(NonProtractorInstance);
Instance.Manage().Timeouts().SetScriptTimeout(TimeSpan.FromSeconds(1000));
Instance.IgnoreSynchronization = false;
However, the following code fails:
Instance.Navigate().GoToUrl(/* URL to angular page */);
With this following error:
Document was unloaded during execution (UnexpectedJavaScriptError)
Note this particular URL does redirect to another page, but both original and redirect page are angular pages.
I've tried every variation of initializing the drivers I could find and they all failed with similar errors.
Any one have any other things I can try to get past this?
Protractor actually does not support FF > v47

Selenium-WebDriver opened the new Firefox window, but didn’t navigate to URL

My source code is copied from selenium docs site. I didn’t make any change.
http://docs.seleniumhq.org/docs/03_webdriver.jsp#user-input-filling-in-forms
I installed Selenium library via NuGet, including
Selenium Remote Control(RC), Selenium WebDriver Mono, Selenium WebDriver Support Classes, Selenium WebDriver, and Selenium WebDriver-backed Selenium.
When I run this code, a new Firefox window was opened. But the Firefox doesn’t navigate to the URL, it just stuck, nothing was loaded.
I tried the Firefox v27, v29, v30 and v31, none of them worked.
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
// Requires reference to WebDriver.Support.dll
using OpenQA.Selenium.Support.UI;
class GoogleSuggest
{
static void Main(string[] args)
{
// Create a new instance of the Firefox driver.
// Notice that the remainder of the code relies on the interface,
// not the implementation.
// Further note that other drivers (InternetExplorerDriver,
// ChromeDriver, etc.) will require further configuration
// before this example will work. See the wiki pages for the
// individual drivers at http://code.google.com/p/selenium/wiki
// for further information.
IWebDriver driver = new FirefoxDriver();
//Notice navigation is slightly different than the Java version
//This is because 'get' is a keyword in C#
driver.Navigate().GoToUrl("http://www.google.com/");
// Find the text input element by its name
IWebElement query = driver.FindElement(By.Name("q"));
// Enter something to search for
query.SendKeys("Cheese");
// Now submit the form. WebDriver will find the form for us from the element
query.Submit();
// Google's search is rendered dynamically with JavaScript.
// Wait for the page to load, timeout after 10 seconds
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until((d) => { return d.Title.ToLower().StartsWith("cheese"); });
// Should see: "Cheese - Google Search"
System.Console.WriteLine("Page title is: " + driver.Title);
//Close the browser
driver.Quit();
}
}
I have had the same problem. the solution is simply uninstall your current firefox browser and download older version "i tried 2010 version 3.6.10 works great". your code is fine the problem is Mozilla people have decided to not give the right to any third party application to control Firefox.
good luck

Categories

Resources