Enabling Chrome Extension in Incognito Mode via CLI flags? - google-chrome

I'm using selenium to test a chrome extension and part of the extension requires the user to be in incognito mode. Currently, I've not been able to enable the extension to be allowed in incognito mode upon startup except by adding the argument user-data-dir=/path/to/directory.
The problem with this is that it loads the extension from the depths of my file system, rather than in a way I can check into git.
I've also tried navigating selenium to the chrome extensions settings page but it seems that selenium can't drive chrome:// pages.
Any ideas on to how to enable incognito on the chrome extension on boot of the chrome driver?

Here is the solution that will work with the latest version of Chrome 74.
Navigate to chrome://extensions
Click on Details button for your desired extension
Copy the url (This contains your extension id)
Now we have to navigate to the above url and then click on the allow in incognito toggle.
Java:
driver.get("chrome://extensions/?id=bhghoamapcdpbohphigoooaddinpkbai");
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.querySelector('extensions-manager').shadowRoot.querySelector('#viewManager > extensions-detail-view.active').shadowRoot.querySelector('div#container.page-container > div.page-content > div#options-section extensions-toggle-row#allow-incognito').shadowRoot.querySelector('label#label input').click()");
Python:
driver.get("chrome://extensions/?id=bhghoamapcdpbohphigoooaddinpkbai")
driver.execute_script("return document.querySelector('extensions-manager').shadowRoot.querySelector('#viewManager > extensions-detail-view.active').shadowRoot.querySelector('div#container.page-container > div.page-content > div#options-section extensions-toggle-row#allow-incognito').shadowRoot.querySelector('label#label input').click()");
Continue Reading, if you want to know how and why
Root Cause:
As part of enhancements to the chrome browser, google moved all the chrome option in to shadow dom. So you can not access allow in incognito toggle element as selenium find_element method which will point to the original dom of the page. So we have to switch to the shadow dom and access the elements in the shadow tree.
Details:
Shadow DOM:
Note: We will be referring to the terms shown in the picture. So please go through the picture for better understanding.
Solution:
In order to work with shadow element first we have to find the shadow host to which the shadow dom is attached. Here is the simple method to get the shadow root based on the shadowHost.
private static WebElement getShadowRoot(WebDriver driver,WebElement shadowHost) {
JavascriptExecutor js = (JavascriptExecutor) driver;
return (WebElement) js.executeScript("return arguments[0].shadowRoot", shadowHost);
}
And then you can access the shadow tree element using the shadowRoot Element.
// get the shadowHost in the original dom using findElement
WebElement shadowHost = driver.findElement(By.cssSelector("shadowHost_CSS"));
// get the shadow root
WebElement shadowRoot = getShadowRoot(driver,shadowHost);
// access shadow tree element
WebElement shadowTreeElement = shadowRoot.findElement(By.cssSelector("shadow_tree_element_css"));
In order to simplify all the above steps created the below method.
public static WebElement getShadowElement(WebDriver driver,WebElement shadowHost, String cssOfShadowElement) {
WebElement shardowRoot = getShadowRoot(driver, shadowHost);
return shardowRoot.findElement(By.cssSelector(cssOfShadowElement));
}
Now you can get the shadowTree Element with single method call
WebElement shadowHost = driver.findElement(By.cssSelector("shadowHost_CSS_Goes_here));
WebElement shadowTreeElement = getShadowElement(driver,shadowHost,"shadow_tree_element_css");
And perform the operations as usual like .click(), .getText().
shadowTreeElement.click()
This Looks simple when you have only one level of shadow DOM. But here, in this case we have multiple levels of shadow doms. So we have to access the element by reaching each shadow host and root.
Below is the snippet using the methods that mentioned above (getShadowElement and getShadowRoot)
// Locate shadowHost on the current dom
WebElement shadowHostL1 = driver.findElement(By.cssSelector("extensions-manager"));
// now locate the shadowElement by traversing all shadow levels
WebElement shadowElementL1 = getShadowElement(driver, shadowHostL1, "#viewManager > extensions-detail-view.active");
WebElement shadowElementL2 = getShadowElement(driver, shadowElementL1,"div#container.page-container > div.page-content > div#options-section extensions-toggle-row#allow-incognito");
WebElement allowToggle = shadowElementL2.findElement(By.cssSelector("label#label input"));
allowToggle.click();
You can achieve all the above steps in single js call as at mentioned at the beginning of the answer (added below just to reduce the confusion).
WebElement allowToggle = (WebElement) js.executeScript("return document.querySelector('extensions-manager').shadowRoot.querySelector('#viewManager > extensions-detail-view.active').shadowRoot.querySelector('div#container.page-container > div.page-content > div#options-section extensions-toggle-row#allow-incognito').shadowRoot.querySelector('label#label input')");

In chrome version 69 this code works (Python version):
driver.get('chrome://extensions')
go_to_extension_js_code = '''
var extensionName = 'TestRevolution';
var extensionsManager = document.querySelector('extensions-manager');
var extensionsItemList = extensionsManager.shadowRoot.querySelector(
'extensions-item-list');
var extensions = extensionsItemList.shadowRoot.querySelectorAll(
'extensions-item');
for (var i = 0; i < extensions.length; i += 1) {
var extensionItem = extensions[i].shadowRoot;
if (extensionItem.textContent.indexOf(extensionName) > -1) {
extensionItem.querySelector('#detailsButton').click();
}
}
'''
enable_incognito_mode_js_code = '''
var extensionsManager = document.querySelector('extensions-manager');
var extensionsDetailView = extensionsManager.shadowRoot.querySelector(
'extensions-detail-view');
var allowIncognitoRow = extensionsDetailView.shadowRoot.querySelector(
'#allow-incognito');
allowIncognitoRow.shadowRoot.querySelector('#crToggle').click();
'''
driver.execute_script(go_to_extension_js_code)
driver.execute_script(enable_incognito_mode_js_code)
Just remember to change var extensionName = 'TestRevolution'; line to your extension name.

If you are trying to enable the already installed extension in incodnito, then try the below code . It should work with chrome.
driver.get("chrome://extensions-frame");
WebElement checkbox = driver.findElement(By.xpath("//label[#class='incognito-control']/input[#type='checkbox']"));
if (!checkbox.isSelected()) {
checkbox.click();
}

I'm still newbie in coding, but I figured another method after looking in chrome's crisper.js at chrome://extensions/ .
First you need to know the extension ID. You can do it by making the id constant here, or using pako's method on obtaining the id's. For mine it's "lmpekldgmhemmmbllpdmafmlofflampm"
Then launch chrome with --incognito and addExtension, then execute the javascript to enable in incognito.
Example:
public class test2 {
static String dir = System.getProperty("user.dir");
static WebDriver driver;
static JavascriptExecutor js;
public static void main(String[] args) throws InterruptedException, IOException{
ChromeOptions options = new ChromeOptions();
options.addArguments("--incognito");
options.addExtensions(new File(dir + "\\randua.crx"));
System.setProperty("webdriver.chrome.driver",dir + "\\chromedriver73.exe");
driver = new ChromeDriver(options);
js = (JavascriptExecutor) driver;
String extID = "lmpekldgmhemmmbllpdmafmlofflampm";
driver.get("chrome://extensions-frame/");
new WebDriverWait(driver, 60).until(webDriver -> js.executeScript("return document.readyState").equals("complete"));
js.executeScript("chrome.developerPrivate.updateExtensionConfiguration({extensionId: \"" + extID + "\",incognitoAccess: true})");
Thread.sleep(1000);
}
}
Hope it helps :)

Related

How to disable chrome pdf viewer in new tab with java in playwright

I'm trying to disable the pdf viewer in a new tab when I use chromium in Playwright. Do you know how can achieve that? Firefox has a method, it's called .setFirefoxUserPrefs().
For chrome, it's not implemented, but found the following thing:
chrome://settings/content/pdfDocuments +++ accessible by playwright.
How it can be implemented?
I tried to take know-how from selenium, but for the playwright, it just doesn't work.
I tried:
how to disable chrome pdf viewer in selenium and it should auto download in the default downloads when any pdf occurs
Disabling PDF Viewer plugin in chromedriver
but without any success, can you support me with that?
Including, I tried that:
private void PlaywrightSmokeTestConfigureInit(String browserType) {
playwright = Playwright.create();
ArrayList<String> arguments = new ArrayList<>();
arguments.add("--start-fullscreen");
boolean headless = false;
Map<String, Object> map = new HashMap<String, Object>();
map.put("pdfjs.disabled", true);
map.put("pdfjs.enabledCache.state", false);
map.put("plugins.plugins_disabled", new String[] { "Chrome PDF Viewer" });
map.put("plugins.always_open_pdf_externally", true);
map.put("profile.default_content_settings.popups", 0);
map.put("safebrowsing.enabled", "true");
switch (browserType) {
case "chromium":
browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setChannel(browserType).setHeadless(headless).setArgs(arguments)
.setFirefoxUserPrefs(map));
break;
case "firefox":
browser = playwright.firefox().launch(new BrowserType.LaunchOptions().setChannel(browserType).setHeadless(headless).setArgs(arguments));
break; ...

Selenium: Clear chrome cache

In my application I need a way to clear only the cache of the chrome browser before log out (except cookies - I do not want to delete cookies).
Can any one suggest me a way to click on the CLEAR DATA button in chrome.
I have written the below code but the code is not working.
Configuration :
Chrome Version: Version 65.0.3325.181 (Official Build) (64-bit)
Selenium Version: 3.11.0
//Clear the cache for the ChromeDriver instance.
driver.get("chrome://settings/clearBrowserData");
Thread.sleep(10000);
driver.findElement(By.xpath("//*[#id='clearBrowsingDataConfirm']")).click();
You are using here
driver.findElement(By.xpath("//*[#id='clearBrowsingDataConfirm']")).click();
Unfortunately, this won’t work because the Chrome settings page uses Polymer and WebComponents, need to use query selector using the /deep/ combinator, so selector in this case is * /deep/ #clearBrowsingDataConfirm.
Here is workaround to your problem...you can achieve the same using either one of the following...
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;
public class ClearChromeCache {
WebDriver driver;
/*This will clear cache*/
#Test
public void clearCache() throws InterruptedException {
System.setProperty("webdriver.chrome.driver","C://WebDrivers/chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("disable-infobars");
chromeOptions.addArguments("start-maximized");
driver = new ChromeDriver(chromeOptions);
driver.get("chrome://settings/clearBrowserData");
Thread.sleep(5000);
driver.switchTo().activeElement();
driver.findElement(By.cssSelector("* /deep/ #clearBrowsingDataConfirm")).click();
Thread.sleep(5000);
}
/*This will launch browser with cache disabled*/
#Test
public void launchWithoutCache() throws InterruptedException {
System.setProperty("webdriver.chrome.driver","C://WebDrivers/chromedriver.exe");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability("applicationCacheEnabled", false);
driver = new ChromeDriver(cap);
}
}
Chrome supports DevTools Protocol commands like Network.clearBrowserCache (documentation).
Selenium does not have an interface for this proprietary protocol by default.
You can add support by expanding Selenium's commands:
driver.command_executor._commands['SEND_COMMAND'] = (
'POST', '/session/$sessionId/chromium/send_command'
)
This is how you use it:
driver.execute('SEND_COMMAND', dict(cmd='Network.clearBrowserCache', params={}))
Note: this example is for Selenium for Python, but it's also possible in Selenium for other platforms in a similar way by expanding the commands.
YEAR 2020 Solution (using Selenium 4 alpha):
Using the devtools
private void clearDriverCache(ChromeDriver driver) {
driver.getDevTools().createSessionIfThereIsNotOne();
driver.getDevTools().send(Network.clearBrowserCookies());
// you could also use
// driver.getDevTools().send(Network.clearBrowserCache());
}
Don´t forget the send keys!!!!
For Selenium Basic, below code is functional.
Function clean_cache()
Set driver = New ChromeDriver
Dim keys As New Selenium.keys
driver.Get "chrome://settings/clearBrowserData"
Sleep 5000
driver.SendKeys (keys.Tab)
Sleep 1000
driver.SendKeys (keys.Tab)
Sleep 1000
driver.SendKeys (keys.Tab)
Sleep 1000
driver.SendKeys (keys.Tab)
Sleep 1000
driver.SendKeys (keys.Tab)
Sleep 1000
driver.SendKeys (keys.Tab)
Sleep 1000
driver.SendKeys (keys.Tab)
Sleep 1000
driver.SendKeys (keys.Enter)
Sleep 2000
driver.Quit
End Function
There is another way to click on Clear data button by traversing through shadow tree. If you are trying to locate clear data button by simply searching web element by locator strategy, it won't work due to Chrome browser version upgrade. You need to traverse through shadow tree. You can try below code to click on "Clear data" in advance tab:
package selenium.demo.test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Test {
static WebDriver driver;
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","F:\\selenium\\chromedriver.exe");
// Instantiate a ChromeDriver class.
driver=new ChromeDriver();
driver.get("chrome://settings/clearBrowserData");
WebElement root1 = driver.findElement(By.cssSelector("settings-ui"));
// get 1st shadowroot element
WebElement shadowRoot1 = expandRootElement(root1);
// get 2nd parent
WebElement root2 = shadowRoot1.findElement(By.cssSelector("settings-main"));
// get 2nd shadowroot element
WebElement shadowRoot2 = expandRootElement(root2);
// get 3rd parent
WebElement root3 = shadowRoot2.findElement(By.cssSelector("settings-basic-page"));
// get 3rd shadowroot element
WebElement shadowRoot3 = expandRootElement(root3);
// get 4th parent
WebElement root4 = shadowRoot3.findElement(By.cssSelector("settings-section > settings-privacy-page"));
// get 4th shadowroot element
WebElement shadowRoot4 = expandRootElement(root4);
// get 5th parent
WebElement root5 = shadowRoot4.findElement(By.cssSelector("settings-clear-browsing-data-dialog"));
// get 5th shadowroot element
WebElement shadowRoot5 = expandRootElement(root5);
// get 6th parent
WebElement root6 = shadowRoot5.findElement(By.cssSelector("#clearBrowsingDataDialog"));
WebElement root7 = root6.findElement(By.cssSelector("cr-tabs[role='tablist']"));
root7.click();
WebElement clearDataButton = root6.findElement(By.cssSelector("#clearBrowsingDataConfirm"));
clearDataButton.click(); // click that hard to reach button!
driver.quit();
}
private static WebElement expandRootElement(WebElement element) {
WebElement ele = (WebElement) ((JavascriptExecutor) driver)
.executeScript("return arguments[0].shadowRoot", element);
return ele;
}
}
Below snippet navigates to the the chrome settings for clearing the browser data and sends a keypress to the opened dialog. Then it waits for the tab to close. You could easily do these steps manual.
IWebDriver Driver = new ChromeDriver()
Driver.Navigate().GoToUrl("chrome://settings/clearBrowserData");
Driver.SwitchTo().ActiveElement();
Driver.FindElement(By.XPath("//settings-ui")).SendKeys(Keys.Enter);
var wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(30));
wait.Until(wd => wd.Url.StartsWith("chrome://settings"));
this way work for me :
in step one =>
pip install keyboard
step2 : use it in your code =>
from time import sleep
self.driver.get('chrome://settings/clearBrowserData')
sleep(10)
keyboard.send("Enter")
Method #1 worked for me in clearing JWT using python selenium and chromedriver 87.
# method 1
driver.execute_script('window.localStorage.clear()')
# method 2
driver.execute_script('window.sessionStorage.clear()')
The correct solution is deprecated, I've solved the problem by following this guide:
https://www.browserstack.com/guide/how-to-handle-cookies-in-selenium#:~:text=Navigate%20to%20the%20chrome%20settings,to%20open%20Chrome%20Developer%20Tools.
Specifically, using the command:
driver.manage().deleteAllCookies();
self.driver.get('chrome://settings/clearBrowserData')
time.sleep(0.5) # this is necessary
actions = ActionChains(self.driver)
actions.send_keys(Keys.TAB * 7 + Keys.ENTER)
actions.perform()
The controling protocol already have this task:
https://chromedevtools.github.io/devtools-protocol/tot/Network/#method-clearBrowserCache
And selenium v4+ have this implemented in its API:
driver.getDevTools().send(Network.clearBrowserCache());
For older versions of selenium it's still possible to call this method natively using underlying protocol:
private void clearCache(ChromeDriverService service, WebDriver driver) throws IOException {
Map<String, Object> commandParams = new HashMap<>();
commandParams.put("cmd", "Network.clearBrowserCache");
commandParams.put("params", emptyMap());
ObjectMapper objectMapper = new ObjectMapper();
HttpClient httpClient = HttpClientBuilder.create().build();
String command = objectMapper.writeValueAsString(commandParams);
String u = service.getUrl().toString() + "/session/" + driver.getSessionId() + "/chromium/send_command";
HttpPost request = new HttpPost(u);
request.addHeader("content-type", "application/json");
request.setEntity(new StringEntity(command));
httpClient.execute(request);
}
Note: for chromium you should use "/chromium/send_command" endpoint, for chrome: "/goog/cdp/execute".
But as of my experience, these both work the same way in both chrome and chromium.
Just add below code
driver.manage().deleteAllCookies();
Getting NoSuchElement exception :
driver = new ChromeDriver(service, chromeOption());
driver.manage().deleteAllCookies();
driver.get("chrome://settings/clearBrowserData");
staticWait(5);
driver.switchTo().activeElement();
driver.findElement(By.cssSelector("* /deep/ #clearBrowsingDataConfirm")).click();
Chrome Version : 97.0.4692.71

How to log in to Chrome with Selenium?

I am testing a Chrome extension which requires the user to be logged in to use, but I cannot figure out how to login with my test account. I have tried logging in to accounts.google.com but this is apparently insufficient; as far as the chrome APIs are concerned there is no authenticated user.
Chrome keeps prompting for login at chrome://chrome-signin but because I can't view the html of the page I can't determine what elements to interact with in Selenium to use it.
You may need to login manually once and then use that for automation.
Try below code , may be it help you:
System.setProperty("webdriver.chrome.driver","<chrome exe path>");
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir= <full local path Google Chrome user data default folder>);
WebDriver driver = new ChromeDriver(options);
driver.get("https://mail.google.com");
Login once manually when browser launched.
Then re-run script now it should use previous login.
Hope it will help you.
I think you can still login automatically. The reason is, when opening the page chrome://chrome-signin, the account textbox is automatically focused, so you just need to use keyboards to login without knowing how the html of the page looks like.
Try the code below (you might need to put some sleep in some places to make sure that everything is loaded properly.
public void loginToChrome(username, password) {
driver.get("chrome://chrome-signin");
var action = new Actions(driver);
action.sendKeys(username).perform();
action.sendKeys(keys.ENTER).perform();
action.sendKeys(password).perform();
action.sendKeys(keys.ENTER).perform();
}
First login to gmail on regular chrome browser (NOT the one triggered by selenium driver). Once you login, install the EditTheCookie extension. And on the gmail tab, click this extension icon. It will give option to copy the cookies in json format to clipboard. Paste that into Gmail.data which will be used in below programme.
Once you past, place that Gmail.data file in a avilable location for below programme (you can place anywhere and update the path of that file in below code accordingly).
I've developed this and is a working solution for me since long.
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Date;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import com.google.gson.Gson;
public class LoginUtils {
private static final String GMAIL_LOGIN_URL =
"https://accounts.google.com/signin/v2/identifier";
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver = LoginUtils.login(driver, GMAIL_LOGIN_URL, "Gmail.data");
}
public static final WebDriver login(WebDriver driver, String url, String pathOfJsonFileName) {
Cookies[] data = readJson(pathOfJsonFileName);
driver.navigate().to(url);
// Set the expire time of each cookie.
Date expiryTime = new Date(System.currentTimeMillis() + 1000000000);
for (Cookies cookie : data) {
Cookie ck = new Cookie(cookie.getName(), cookie.getValue(), cookie.getDomain(), cookie.getPath(),
expiryTime, Boolean.parseBoolean(cookie.getSecure()), Boolean.parseBoolean(cookie.getHttpOnly()));
driver.manage().addCookie(ck);
}
return driver;
}
private static final Cookies[] readJson(String jsonFileName) {
String json = null;
try {
byte[] encoded = Files.readAllBytes(Paths.get(jsonFileName));
json = new String(encoded);
} catch (Exception e) {
e.printStackTrace();
}
return new Gson().fromJson(json, Cookies[].class);
}
}

C# Get full html document from site

I tried to use GetStringAsync
using (var client = new HttpClient())
{
var html = await client.GetStringAsync(url);
richTextBox1.Text = html.ToString();
}
and DownloadString
System.Net.WebClient wc = new System.Net.WebClient();
string webData = wc.DownloadString(url);
richTextBox1.Text = webData;
But it doesn't give me full html document like Google Chrome F12. How can I get full html code of url using C#?
Need this url: http://poeplanner.com/ but it doesn't show me even a single table when Chrome F12 does.
My guess is that the code you don't see is a code that added with javascript. So you need use a browser program to get this code too.
This app will run the javascript too and you can ask from it the final html.
If I'm right, try to use phantomjs.
Related question on PhantomJS

How do you automatically open the Chrome Devtools tab within Selenium (C#)?

I see that there's a relatively new option to open Chrome with Devtools open from the command line, which I have gotten to work from my Windows 8.1 command line using a call like this:
c:\Program Files (x86)\Google\Chrome\Application>"chrome.exe" --auto-open-devtools-for-tabs
When I try to add this option on the same box when creating my ChromeDriver in Selenium (in C#), however, the option seems to be ignored.
var options = new ChromeOptions();
options.AddArgument("auto-open-devtools-for-tabs");
string executingAssembly = System.Reflection.Assembly.GetExecutingAssembly().Location;
string driverPath = Path.Combine(Path.GetDirectoryName(executingAssembly), "ChromeWebDriver");
_driver = new ChromeDriver(driverPath, options);
I've tried a few variations on theme to make sure options are working at all, including...
var options = new ChromeOptions();
options.AddArguments(new[] { "start-maximized", "auto-open-devtools-for-tabs"});
... and...
var options = new ChromeOptions();
options.AddArgument("start-maximized");
options.AddArgument("auto-open-devtools-for-tabs");
... and...
var options = new ChromeOptions();
options.AddArgument("start-maximized");
options.AddExcludedArgument("auto-open-devtools-for-tabs");
... as well as setting those with -- in front of each option string. All I get from any of those are maximized windows.
I get the feeling the auto-open-devtools-for-tabs argument's not supported by Selenium's Chrome Web Driver, but I'm not sure why that wouldn't support the same set of options as the "full" app.
Anyone have this option working with Selenium in C#, or know why it shouldn't be working in this case?
This is not unlike this question, but here I'm asking specifically about the auto-open-devtools-for-tabs option with C#. That asker claims not to have had any luck with options, and was asking how to open devtools from "within" Selenium, looking for a method explicitly before this option existed.
I've tried this with in VS 2017, Selenium v3.12.1#, Firefox v60.0.2, Chrome v66, Nunit v3.10.1, Gecko Driver v20.1, and Chrome driver v2.4 (all using C#).
I tried to search for Firefox but did not have any success. I did find a solution for Chrome v66.
Please provide profile like this: options.AddArguments("--auto-open-devtools-for-tabs");
This is a complete chrome driver implementation:
ChromeOptions options = new ChromeOptions();
options.AddArgument("--start-maximized");
options.AddArguments("disable-infobars");
options.AddArguments("--disable-notifications");
options.AddArguments("--auto-open-devtools-for-tabs");
driver = new ChromeDriver(DrivePath, options, TimeSpan.FromSeconds(100));
See also this post: "List of Chromium Command Line Switches"
Below commands are NOT working, this is issue with Geckodriver so Gecko team has to provide some solution or fix for that:
driver.FindElement(By.CssSelector("body")).SendKeys(Keys.F12);
Actions action = new Actions(driver); action.SendKeys(Keys.F12); action.Perform();
Actions action = new Actions(driver); action .KeyDown(Keys.Control)
.SendKeys(Keys.F12).KeyUp(Keys.Control).Perform();
Actions action = new Actions(driver); action.SendKeys(Keys.F12); action.Click();
Following the thread on SO-12212504 and leading from the selected answer.
One of the solution to this would be pressing F-12 [Key F12 Documentation] key using :
// without an element
new Actions(driver).SendKeys(Keys.F12).Perform();
// send keys to body
new Actions(driver).SendKeys(driver.FindElement(By.XPath("//body")), Keys.F12).Perform();
On the other side could you try and use AddUserProfilePreference from amongst the ChromeOptions Methods :
ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("auto-open-devtools-for-tabs", "true");
Note : I am not very sure about the parameter name, but I hope you can find something corresponding here.
Edit : Some more attempts using keyboard shortcuts for the same -
Windows : [F12 or Ctrl + Shift + I]
String openDevTools = Keys.chord(Keys.CONTROL, Keys.SHIFT, "I");
driver.FindElement(By.XPath("//body")).SendKeys(openDevTools).Perform();
Mac : [Cmd + Opt + I]
String openDevTools = Keys.chord(Keys.COMMAND, Keys.ALT, "I");
driver.FindElement(By.XPath("//body")).SendKeys(openDevTools).Perform();
Ruby: must have installed latest selenium-webdriver (3.7.0) gem
options1 = Selenium::WebDriver::Chrome::Options.new
options1.add_argument('--auto-open-devtools-for-tabs')
driver = Selenium::WebDriver.for :chrome, options: options1
driver.get("https://stackoverflow.com")
I think the issue is with your options being declared as a var and not ChromeOptions, this code open google.com with dev tools open
public static void Scraps()
{
//Declare options variable and set dev tools argument
ChromeOptions co = new ChromeOptions();
co.AddArguments("--auto-open-devtools-for-tabs");
//Initiate driver instance and go to google.com
IWebDriver driver = new ChromeDriver(co);
driver.Url = "https://www.google.com/";
}