C# Selenium/ChromeDriver Add User Profile Preference - c#

I need to allow all cookies when running tests with selenium + chrome driver.
I am trying to add this as a profile preference using ChromeOptions.AddUserProfilePreference
I'm not 100% sure what the preference name should be to allow all cookies. I have referenced this doc https://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/pref_names.cc?view=markup
and have tried the following in my setup but its not having the desired effect.
options.AddUserProfilePreference("profile.block_third_party_cookies", false);
options.AddUserProfilePreference("security.cookie_behavior", 0);```
Here is my setup code
new DriverManager().SetUpDriver(new ChromeConfig());
var options = new OpenQA.Selenium.Chrome.ChromeOptions { };
options.AddArgument("–no-sandbox");
options.AddArguments("-disable-gpu");
options.AddArguments("-disable-dev-shm-usage");
options.AddArgument("-incognito");
options.AddArgument("-start-maximized");
options.AddUserProfilePreference("security.cookie_behavior", 0);
CurrentWebDriver = new ChromeDriver(options);

I ran into the same issue. I found that using the following helped me:
options.AddUserProfilePreference("profile.cookie_controls_mode", 0);
The advice that helped me find this, was to check the Chrome preferences file (in my case C:\Users\<user>\AppData\Local\Google\Chrome\User Data\Default\Preferences). I saved a copy with cookies blocked, then changed the setting to allow all cookies and compared the two versions, and that highlighted the affected control for me.

Looks like in Chrome v86 third-party cookies disabled by default: Chromium SameSite Updates.
I found a workaround to enable third-party cookies on the browser new tab start page (I'm using inputsimulator library to open a new tab because other methods do not open the appropriate page).
Here is C# code for IWebDriver extension:
using WindowsInput;
using WindowsInput.Native;
public static void EnableThirdPartyCookies(this IWebDriver driver)
{
var windowHandles = driver.WindowHandles;
// Activate Browser window
driver.SwitchTo().Window(driver.WindowHandles.Last());
// Open New Tab Ctrl + T
new InputSimulator().Keyboard.ModifiedKeyStroke(VirtualKeyCode.CONTROL, VirtualKeyCode.VK_T);
// Wait for open new tab
const int retries = 100;
for (var i = 0; i < retries; i++)
{
Thread.Sleep(100);
if (driver.WindowHandles.Count > windowHandles.Count)
break;
}
// Enable Third Party Cookies
if (driver.WindowHandles.Count > windowHandles.Count)
{
driver.Close();
driver.SwitchTo().Window(driver.WindowHandles.Last());
var selectedCookieControlsToggle = driver.FindElements(By.Id("cookie-controls-toggle"))
.FirstOrDefault(x => x.GetAttribute("checked") != null);
selectedCookieControlsToggle?.Click();
}
}
Using:
var chromeService = ChromeDriverService.CreateDefaultService();
var options = new ChromeOptions { };
options.AddArgument("–no-sandbox");
options.AddArgument("-incognito");
options.AddArgument("-start-maximized");
var driver = new ChromeDriver(chromeService, options);
driver.EnableThirdPartyCookies();
Here is Java code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public void enableThirdPartyCookies(WebDriver driver) throws Exception {
ArrayList<String> windowHandles = new ArrayList<String> (driver.getWindowHandles());
// Activate Browser window
driver.switchTo().window(driver.getWindowHandle());
// Open New Tab Ctrl + T
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_CONTROL);
// Wait for open new tab
int retries = 100;
for (int i = 0; i < retries; i++)
{
Thread.sleep(100);
if (driver.getWindowHandles().size() > windowHandles.size())
break;
}
// Enable Third Party Cookies
if (driver.getWindowHandles().size() > windowHandles.size())
{
driver.close();
windowHandles = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(windowHandles.get(windowHandles.size() - 1));
List list = driver.findElements(By.id("cookie-controls-toggle"));
Optional<WebElement> selectedCookieControlsToggle = driver.findElements(By.id("cookie-controls-toggle")).stream()
.filter(x -> x.getAttribute("checked") != null).findFirst();
Optional.ofNullable(selectedCookieControlsToggle).get().get().click();
}
}
Using:
ChromeOptions options = new ChromeOptions();
options.addArguments("–no-sandbox");
options.addArguments("incognito");
options.addArguments("start-maximized");
WebDriver driver = new ChromeDriver(options);
enableThirdPartyCookies(driver);

Related

Selenium Chrome does not add extensions and arguments

It looks like Selenium doesn't apply any options to the new browser instance, since neither the extension nor the arguments that were defined in code are applied, it just launches a regular Chrome window without extensions, with an infobar and no incognito mode
void ChromeSession()
{
const string siteUrl = "https://www.google.com/";
var options = new ChromeOptions();
options.AddArgument("--disable-infobars");
options.AddArgument("--incognito");
options.AddExtension(#"C:\Users\kiespetch1\Downloads\Solver.crx");
new DriverManager().SetUpDriver(new ChromeConfig());
var driver = new ChromeDriver();
driver.Url = siteUrl;
}
How I can add extensions and launch arguments?
You can add profiles
options.AddArgument = ("user-data-dir':'/Users/Application/Chrome/Default'}

Frame # not found when using Puppeteer

I'm having issues with Puppeteer, I am trying to type in a textbox that is in an IFrame.
I have created a simple repo with a code snippet, this one contains an IFrame with a tweet from Twitter.
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultChromiumRevision);
var launchOptions = new LaunchOptions
{
Headless = false,
DefaultViewport = null
};
launchOptions.Args = new[] { "--disable-web-security", "--disable-features=IsolateOrigins,site-per-process" };
ChromeDriver = await Puppeteer.LaunchAsync(launchOptions);
page = await ChromeDriver.NewPageAsync();
await page.GoToAsync(Url, new NavigationOptions { WaitUntil = new WaitUntilNavigation[] { WaitUntilNavigation.Networkidle0 } });
var selectorIFrame = "#twitter_iframe";
var frameElement1 = await page.WaitForSelectorAsync(selectorIFrame);
var frame1 = await frameElement1.ContentFrameAsync();
var frameContent1 = await frame1.GetContentAsync();
var frame1 = await frameElement1.ContentFrameAsync(); fails with Frame # not found, see image with error below.
Versions:
PuppeteerSharp 7.0
.Net Framework 6
Git example
Try to disable some of the security features that can be disabled when launching puppeteer.
Check in puppeteer chrome://flags/ in case there's something blocking iframe access, maybe is insecure content or maybe you have to be explicit about isolation trial.
My 2 cents on this, it should allow it to access it from non secure
Args = new[]
{
"--disable-web-security",
"--disable-features=IsolateOrigins,site-per-process,BlockInsecurePrivateNetworkRequests",
"--disable-site-isolation-trials"
}

How can I use click event on multiple page in the same test cases on Selenium and C#

How can I use click event on multiple page in the same test cases on Selenium and C#
For example:
Go to Google Homepage.
Search the resulting Test
Click on the Search button,
The user gets Redirected to the Searched Result page, but when I am trying to select any link it showing error.
public void opengoogle()
{
ChromeOptions option = new ChromeOptions();
option.AddArgument("--headless");
ChromeDriver wd = new ChromeDriver(option);
try
{
wd.Navigate().GoToUrl("https://www.google.co.in/");
Thread.Sleep(2000);
wd.FindElement(By.CssSelector("#lst-ib")).Click();
Thread.Sleep(2000);
wd.FindElement(By.CssSelector("#lst-ib")).Click();
wd.FindElement(By.CssSelector("#lst-ib")).Clear();
wd.FindElement(By.CssSelector("#lst-ib")).SendKeys("Test");
wd.FindElement(By.CssSelector("#lst-ib")).Submit();
}
finally { }
public void TestMethod1()
{
ChromeOptions option = new ChromeOptions();
option.AddArgument("--headless");
ChromeDriver webDriver = new ChromeDriver(option);
try
{
webDriver.Navigate().GoToUrl("https://www.google.lk/");
WebDriverWait wait = new WebDriverWait(webDriver, new TimeSpan(0, 0, 0, 30));
IWebElement searchTextField= wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("lst-ib")));
searchTextField.SendKeys("Test");
IWebElement searchButton = wait.Until(ExpectedConditions.ElementToBeClickable(By.CssSelector(".lsb")));
searchButton.Click();
wait.Until(ExpectedConditions.TitleContains("Test"));
IWebElement searchResultLink= wait.Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("a[href='http://www.speedtest.net/']")));
searchResultLink.Click();
}
finally { }
}
You can try above code.
It explains following process.
First move to Google search page.
Insert "Test" in to search text field.
Then click on Google search button.
After Navigate to search results page, click on "www.speedtest.net" link.
You can try out this code:
ChromeOptions option = new ChromeOptions();
option.AddArgument("--headless");
ChromeDriver wd = new ChromeDriver(option);
wd.Navigate().GoToUrl("https://www.google.co.in/");
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromMinutes(1));
wait.Until(ExpectedConditions.ElementToBeClickable(By.Name("q")));
wd.FindElement(By.Name("q")).SendKeys("Test"+Keys.RETURN);
wait.Until(ExpectedConditions.VisibilityOfElementLocated(By.CssSelector("a[href='http://www.speedtest.net/']")));
driver.FindElement(By.CssSelector("a[href='http://www.speedtest.net/']")).Click();
P.S :
1. I'm not good in C# though I've tried to convert a java code to C#. Hope this will be helpful for you.
2. This will always open the speedtest.net website. If you want something else, you can have it by replacing the cssSelector which I have written.

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

Disable images in Selenium Google ChromeDriver

How does one disable images in Google chrome when using it through Selenium and c#?
I've attempted 6 ways and none worked. I've even tried the answer on this StackOverflow question, however I think the info in it is out of date.
Chrome driver: V2.2
Chrome version: V29.0.1547.66 m
Selenium: V2.35
All the attempts I've made don't cause exceptions, they run normally but still display images:
Attempt 1:
ChromeOptions co = new ChromeOptions();
co.AddArgument("--disable-images");
IWebDriver driver = new ChromeDriver(co);
Attempt 2:
DesiredCapabilities capabilities = DesiredCapabilities.Chrome();
capabilities.SetCapability("chrome.switches", new string[1] { "disable-images" });
Attempt 3:
ChromeOptions co = new ChromeOptions();
co.AddAdditionalCapability("chrome.switches", new string[1] { "disable-images" });
Attempt 4:
var imageSetting = new Dictionary<string, object>();
imageSetting.Add("images", 2);
Dictionary<string, object> content = new Dictionary<string, object>();
content.Add("profile.default_content_settings", imageSetting);
var prefs = new Dictionary<string, object>();
prefs.Add("prefs", content);
var options = new ChromeOptions();
var field = options.GetType().GetField("additionalCapabilities", BindingFlags.Instance | BindingFlags.NonPublic);
if (field != null)
{
var dict = field.GetValue(options) as IDictionary<string, object>;
if (dict != null)
dict.Add(ChromeOptions.Capability, prefs);
}
Attempt 5:
ChromeOptions options = new ChromeOptions();
options.AddAdditionalCapability("profile.default_content_settings", 2);
Attempt 6:
Dictionary<String, Object> contentSettings = new Dictionary<String, Object>();
contentSettings.Add("images", 2);
Dictionary<String, Object> preferences = new Dictionary<String, Object>();
preferences.Add("profile.default_content_settings", contentSettings);
DesiredCapabilities caps = DesiredCapabilities.Chrome();
caps.SetCapability("chrome.prefs", preferences);
This is my solution
IWebDriver driver;
ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("profile.default_content_setting_values.images", 2);
driver = new ChromeDriver(options);
You should use --blink-settings instead of --disable-images by:
options.add_argument('--blink-settings=imagesEnabled=false')
which also works in headless mode. Setting profile doesn't work in headless mode. You can verify it by screenshot:
driver.get_screenshot_as_file('headless.png')
Note: I was using python with selenium, but I think it should be easy to transfer to c#.
For your method 1-3, I don't see a Chrome switch called --disable-images listed here. So even if the code snippets are correct, they won't work no matter what. Where did you get that switch? Any references?
For the methods 4-6, I assume you got the idea from this chromdriver issue. I don't know if this {'profile.default_content_settings': {'images': 2}} is still valid or not, but you can give it a try with the following code (which was originally the answer to How to set Chrome preferences using Selenium Webdriver .NET binding?, answer provided by Martin Devillers).
public class ChromeOptionsWithPrefs: ChromeOptions {
public Dictionary<string,object> prefs { get; set; }
}
public static void Initialize() {
var options = new ChromeOptionsWithPrefs();
options.prefs = new Dictionary<string, object> {
{ "profile.default_content_settings", new Dictionary<string, object>() { "images", 2 } }
};
var driver = new ChromeDriver(options);
}
Use http://chrome-extension-downloader.com/ to download the "Block Image" extension (https://chrome.google.com/webstore/detail/block-image/pehaalcefcjfccdpbckoablngfkfgfgj?hl=en-GB). The extension prevents the image from being downloaded in the first place. Now it's just a matter of loading it using the following statement:
var options = new ChromeOptions();
//use the block image extension to prevent images from downloading.
options.AddExtension("Block-image_v1.0.crx");
var driver = new ChromeDriver(options);
An easier approach would be solely:
ChromeOptions options = new ChromeOptions();
options.addArguments("headless","--blink-settings=imagesEnabled=false");
I found out simple solution. This idea referred from Python:Disable images in Selenium Google ChromeDriver
var service = ChromeDriverService.CreateDefaultService(#WebDriverPath);
var options = net ChromeOptions();
options.AddUserProfilePreference("profile.managed_default_content_settings.images", 2);
IWebDriver Driver = new ChromeDriver(service, options);
I recommend that you create a new profile, customize this profile so that it does not load the images, and use this profile as a chromeOption to set up your driver.
See: this article

Categories