如何自动在Selenium(C#)中打开Chrome Devtools选项卡? [英] How do you automatically open the Chrome Devtools tab within Selenium (C#)?

查看:747
本文介绍了如何自动在Selenium(C#)中打开Chrome Devtools选项卡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到一个相对较新的选择 ="https://stackoverflow.com/a/36957422/1028230">使用从命令行打开的Devtools来打开Chrome ,我已经使用以下调用从Windows 8.1命令行运行了该工具:

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

但是,当我尝试在Selenium(在C#中)创建我的ChromeDriver时,在同一框上添加此选项时,该选项似乎被忽略了.

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"});

...和...

var options = new ChromeOptions();
options.AddArgument("start-maximized");
options.AddArgument("auto-open-devtools-for-tabs");

...和...

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.

我感到Selenium的Chrome Web驱动程序不支持auto-open-devtools-for-tabs参数,但是我不确定为什么它不支持与完整"应用程序相同的选项集.

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.

任何人都可以使用C#中的Selenium来使用此选项,还是知道为什么在这种情况下不应该使用它?

Anyone have this option working with Selenium in C#, or know why it shouldn't be working in this case?

这与此问题没什么不同,但是在这里我要特别问一下C#中的auto-open-devtools-for-tabs选项.那个问问者声称没有任何带有选项的运气,并询问如何从"Selenium中"打开devtools,在此选项存在之前明确地寻找一种方法.

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.

推荐答案

我已经在VS 2017,Selenium v​​3.12.1#,Firefox v60.0.2,Chrome v66,Nunit v3.10.1,Gecko Driver v20中进行了尝试. 1和Chrome驱动程序v2.4(均使用C#).

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#).

我尝试搜索Firefox,但没有成功.我确实找到了适用于Chrome v66的解决方案.

I tried to search for Firefox but did not have any success. I did find a solution for Chrome v66.

请提供以下个人资料:options.AddArguments("--auto-open-devtools-for-tabs");

Please provide profile like this: options.AddArguments("--auto-open-devtools-for-tabs");

这是完整的chrome驱动程序实现:

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));

另请参阅此帖子:" Chromium命令行开关列表 "

See also this post: "List of Chromium Command Line Switches"

以下命令不起作用,这是Geckodriver的问题,因此Gecko团队必须为此提供一些解决方案或解决方案:

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();

这篇关于如何自动在Selenium(C#)中打开Chrome Devtools选项卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆