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

4

12

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.

Mouthwatering answered 7/6, 2016 at 15:31 Comment(0)
A
18

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();
Airtoair answered 12/6, 2018 at 21:58 Comment(0)
G
2

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();
Grindlay answered 7/6, 2016 at 17:53 Comment(2)
No dice on AddUserProfilePreference. Worth a shot; thanks. But no devtools when I run it, same as before. And though it's a different route, I did try SendKeys, but it only opened devtools intermittently. Worse, the times it does work, I get kinda what I thought I might, disconnected: received Inspector.detached event\n (Session info: chrome=50.0.2661.102) (Driver info: chromedriver=2.21.371459... So no luck yet.Mouthwatering
Oh okay, probably the flag auto-open-devtools-for-tabs shall exist just the way to use it is not documented anywhere. https://mcmap.net/q/86146/-automatically-open-chrome-developer-tools-when-new-tab-new-window-is-openedGrindlay
C
1

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")
Crossbred answered 23/11, 2017 at 10:19 Comment(0)
C
0

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/";
   }
Cardona answered 11/3, 2022 at 22:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.