Selenium C# to disable Microsoft Chromium Edge browser Sync pop-up - c#

I am trying to test automate my application website using selenium and C# on Chromium Edge browser (version 83.0.478.45).
Every time the chromium edge driver opens up browser, it displays a pop up for sync as shown in picture below. Is there any way to stop it?
EdgeOptions used:
options.UseChromium = true;
options.AddArguments("disable-infobars");
options.AddUserProfilePreference("disable-popup-blocking", "true");
options.AddArguments("--disable-web-security");

As discussed in the comments, You can try to launch the MS Edge browser using a default profile that can help you to fix this issue.
using OpenQA.Selenium.Edge;
using System.Threading;
namespace ecwebdriver
{
public class edgewebdriver
{
static void Main(string[] args)
{
EdgeOptions edgeOptions = new EdgeOptions();
edgeOptions.UseChromium = true;
edgeOptions.addArguments("user-data-dir=C:\\Users\\username\\AppData\\Local\\Microsoft\\Edge\\User Data");
var msedgedriverDir = #"E:\webdriver";
var driver = new EdgeDriver(msedgedriverDir, edgeOptions);
driver.Navigate().GoToUrl("<website url>");
Thread.Sleep(3000);
driver.Close();
}
}
}
Sample code that modified by op.
var userDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft\\Edge\\User Data");

Related

Selenium C# - While running - How should i skip the login if already logged in

Here the login code:
public void Valid_login()
{
Config config = new Config();
Login_methods login = new Login_methods();
string log_Messgae = login.Login(config.Username, config.password, config.companyID);
Assert.AreEqual("Success", log_Messgae);
if (log_Messgae == "Success")
Logged_status = "logged";
else
Logged_status = "loggedoff";
}
Here i used conditional statement but it is not working. When running the below code for each and every testcase the browser is launched and going to login page, even if i´m allready logged in.
public void Req_Search()
{
Config config = new Config();
Menus menu = new Menus();
Login_methods login = new Login_methods();
if (loginpage.Logged_status == "logged")
{
string current_Url = Driver.driver.Url;
if (!current_Url.Contains("requisition/requisition-search"))
menu.Navigate_Requisition_search();
}
else
{
login.Initilize_Driver();
loginpage.Valid_login();
menu.Navigate_Requisition_search();
}
}
When opening the the browser, selenium does not open the browser with your profile and instead opens with a default profile that has zero data about your past uses. You can use the following code to open chrome browser with your profile instead of the default profile
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace SeleniumCore
{
public class Tests
{
static void Main(String[] args)
{
string currentUser = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
ChromeOptions options = new ChromeOptions();
options.AddArgument($"user-data-dir={currentUser}/AppData/Local/Google/Chrome/User Data");
IWebDriver webDriver = new ChromeDriver(options);
webDriver.Navigate().GoToUrl("https://www.google.com");
}
}
}
Its because selenium open your default chrome profile.
driver.AddArguments(#"--user-data-dir=C:\Users\*username*\AppData\Local\Google\Chrome\User Data");
driver.AddArguments(#"--profile-directory=*name of your profile*");
After it will load this profile cookies.
If you're running on headless, dont know why but selenium seem unable to use profiles.

C# Selenium - Failed to Start Tor

I'm trying to launch Tor browser through Selenium in C# using the following code:
using OpenQA.Selenium.Firefox;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace AutomatorApp
{
public class BrowserAutomator
{
public void Automate()
{
String torPath = "D:\\Tor Browser\\Browser\\firefox.exe";
String profilePath = "D:\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default\\";
FirefoxProfile profile = new FirefoxProfile(profilePath);
profile.SetPreference("network.proxy.type", 1);
profile.SetPreference("network.proxy.socks", "127.0.0.1");
profile.SetPreference("network.proxy.socks_port", 9153);
profile.SetPreference("network.proxy.socks_remote_dns", false);
FirefoxDriverService firefoxDriverService = FirefoxDriverService.CreateDefaultService("D:\\geckodriver-v0.26.0-win64", "geckodriver.exe");
firefoxDriverService.FirefoxBinaryPath = torPath;
var firefoxOptions = new FirefoxOptions
{
Profile = profile,
LogLevel = FirefoxDriverLogLevel.Trace
};
FirefoxDriver driver = new FirefoxDriver(firefoxDriverService, firefoxOptions);
}
}
}
However, this shows the error 'Tor Failed to Start' and the exception simply contains 'Permission denied'. I tried launching the app in administrator mode and ensuring that the folder is accessible by all users but this did not solve the issue.
Interestingly, when I try to launch a Firefox browser using the same setup it works well.
Any help is very much appreciated.
Update - Solved: After updating to the latest Tor version (9.5.1) The final working code:
FirefoxProfile profile = new FirefoxProfile(profilePath);
profile.SetPreference("network.proxy.type", 1);
profile.SetPreference("network.proxy.socks", "127.0.0.1");
profile.SetPreference("network.proxy.socks_port", 9153);
profile.SetPreference("network.proxy.socks_remote_dns", false);
FirefoxDriverService firefoxDriverService = FirefoxDriverService.CreateDefaultService(geckoDriverDirectory);
firefoxDriverService.FirefoxBinaryPath = torPath;
firefoxDriverService.BrowserCommunicationPort = 2828;
var firefoxOptions = new FirefoxOptions
{
Profile = null,
LogLevel = FirefoxDriverLogLevel.Trace
};
firefoxOptions.AddArguments("-profile", profilePath);
FirefoxDriver driver = new FirefoxDriver(firefoxDriverService, firefoxOptions);
driver.Navigate().GoToUrl("https://www.google.com");
Important notes:
The following TOR configs need to be changed in about:config :
marionette.enabled: true
marionette.port: set to an unused port, and set this value to firefoxDriverService.BrowserCommunicationPort in your code. This was set to 2828 in my example.
As far as I remember from my attempts a few years ago, TOR with WebDriver didn't work when you set that "Profile" option. Normal Firefox works, but TOR simply doesn't.
If you get a timeout error after this, make sure marionette is enabled on about:config. If it's already enabled, follow what is going on with TOR on start up. Like if the actual firefox browser doesn't load up, stuck at connection launhcer etc, or there is a message box at the browser startup or something... And also make sure no other firefox.exe, geckodriver.exe or tor.exe is running on the background. If multiple executubles are not configured to listen different port numbers, it might cause problems.
Environment.SetEnvironmentVariable("webdriver.gecko.driver", "C:\\TorBrowser\\Browser\\geckodriver.exe");
var gekcoService = FirefoxDriverService.CreateDefaultService("C:\\TorBrowser\\Browser", "geckodriver.exe");
var gekcoService.FirefoxBinaryPath = "C:\\TorBrowser\\Browser\\firefox.exe";
// marionette port that browser listens to. I had it set to a custom port on about:config
var gekcoService.BrowserCommunicationPort = 50111;
// also had given a custom port for geckodriver listen port
var gekcoService.Port = 9881;
var gekcoService.Host = 127.0.0.1;
var gekcoService.HostName = 127.0.0.1;
var gekcoService.Start();
var ffOptions = new FirefoxOptions
{
AcceptInsecureCertificates = true,
BrowserExecutableLocation = "C:\\TorBrowser\\Browser\\firefox.exe",
Profile = null,
UnhandledPromptBehavior = UnhandledPromptBehavior.Dismiss
};
ffOptions.AddArguments("-profile", "C:\\TorBrowser\\Browser\\TorBrowser\\Data\\Browser\\profile.default");
ffOptions.LogLevel = FirefoxDriverLogLevel.Info;
ffOptions.PageLoadStrategy = PageLoadStrategy.Eager;
var ffDriver = new FirefoxDriver(gekcoService, ffOptions);
ffDriver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(10);
ffDriver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(10);
ffDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
Your code block looks perfect to me.
However there seems to be an issue opening the tor Browser 9.5 which uses the default Firefox v68.9.0esr.
You can find a detailed discussion in How to initiate a Tor Browser 9.5 which uses the default Firefox to 68.9.0esr using GeckoDriver and Selenium through Python
Solution
The solution will be to install and use either of the following browsers:
Firefox v77.0.1
Firefox Nightly v79.0a1
In case you use the above mentioned browsers, you need to:
Firefox v77.0.1:
String torPath = "C:\\Program Files\\Mozilla Firefox\\firefox.exe";
Firefox Nightly v79.0a1
String torPath = "C:\\Program Files\\Firefox Nightly\\firefox.exe";

Internet Explorer 11 Click problem on Xunit

I am using Windows 10 with IE11 on selenium XUNIT test. Selenium doesn't click at a desired IE element. Can someone suggest a solution please?
We had similar problem back in the day when the change the webdriver for IE, whit this code I can run it only in some computers, because in another's we have a lost of comunication between IE and the driver/code due another problem.
public static IWebDriver ExplorerDriverSetUp()
{
var options = new InternetExplorerOptions();
options.RequireWindowFocus = false;
options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
options.IgnoreZoomLevel = true;
options.EnsureCleanSession = true;
options.InitialBrowserUrl = URL;
IWebDriver driver = new InternetExplorerDriver(options);
driver.Manage().Window.Maximize();
return driver;
}

I am unable to resolve the below problem. This is the initial start page for the WebDriver server

This is the initial start page for the WebDriver server observed on IE with different localhost and port number shown on my screen.
I have put my code sample below. Please help me out.
IE version: 11 (32 bit)
Selenium IE Webdriver : 3.141.5.0 (32 bit)
C# language
I tried solutions given in stack overflow by Enabling all protected zones.
public static void Main(string[] args)
{
IWebDriver driver = new InternetExplorerDriver();
string url = #"http://www.google.com";
driver.Navigate().GoToUrl(url);
Thread.Sleep(10000);
Console.WriteLine("Ending");
driver.Quit();
}
It is expected to open google.com. But shows me This is the initial start page for the WebDriver server.
Please make sure you have downloaded the "IEDriverServer.exe" first, then, you could use the following code to use the webdriver:
private const string URL = #"http://www.google.com";
// DriverServer path. You could download the server from http://selenium-release.storage.googleapis.com/index.html. then get the path.
private const string IE_DRIVER_PATH = #"D:\Downloads\webdriver\IEDriverServer_x64_3.14.0";
static void Main(string[] args)
{
var options = new InternetExplorerOptions()
{
InitialBrowserUrl = URL,
IntroduceInstabilityByIgnoringProtectedModeSettings = true
};
var driver = new InternetExplorerDriver(IE_DRIVER_PATH, options);
driver.Navigate();
driver.Close(); // closes browser
driver.Quit(); // closes IEDriverServer process
Console.ReadKey();
}
The selenium webdriver was not compatible with the browser. It needed re-installation of the webdriver to match browser. It works fine now.

Unable to open chrome with Webdriver in C#

Have gone through previous post, but the error I am facing is different.
Trying to open chrome through webdriver using C#.
namespace HelloWorld
{
public class OurFirstTest
{
static void main(String[] args)
{
IWebDriver driver = new ChromeDriver(#"D:\Automation\chromedriver");
driver.Navigate().GoToUrl("http://www.google.com");
}
}
}
During build, command prompt opens with message
Starting ChromeDriver <v2.9.248315> on port 9515.
Browser is not opening....
I Edited my code and you can follow it now, I am using this code to run chrome instance in incognito mode.
IWebDriver driver1;
ChromeOptions m_Options = new ChromeOptions();
m_Options.AddArgument("--user-data-dir=C:/Users/dell/AppData/Local/Google/Chrome/User Data/Profile 2");
m_Options.AddArgument("--disable-extensions");
m_Options.AddArgument("--silent");
m_Options.AddArgument("--incognito");
//Adding a Proxy
Proxy proxy = new Proxy();
proxy.HttpProxy = "XXXX.XXX.X.X:XXXX";
m_Options.Proxy = proxy;
driver1 = new ChromeDriver(#"F:\\ChromeDriver\", m_Options);
driver1.Navigate().GoToUrl("https://www.google.com");
Make sure you set up the right path for driver.
You'd better put your chromediver in the same directory with your test's exe file.
And update your chromedriver to the latest version which is 2.10

Categories