Java Chrome 正在被自动化测试软件控制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43143014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:43:28  来源:igfitidea点击:

Chrome is being controlled by automated test software

javagoogle-chromeseleniumselenium-chromedriverserenity-bdd

提问by BobbyB

I am running automated tests in Chrome with Serenity BDD (Selenium).

我正在 Chrome 中使用 Serenity BDD (Selenium) 运行自动化测试。

I had to download a new ChromeDriver, because my tests could not run -> The test would open ChromeDriver but could not "Browse as user". When I googled the issue, they said I had to update ChromeDriver.

我不得不下载一个新的 ChromeDriver,因为我的测试无法运行 -> 测试将打开 ChromeDriver 但无法“以用户身份浏览”。当我用谷歌搜索这个问题时,他们说我必须更新 ChromeDriver。

So I updated ChromeDriver to version 2.28 and I also updated the Chrome version to Version 57.0.2987.98.

所以我将 ChromeDriver 更新为 2.28 版,并将 Chrome 版本更新为 57.0.2987.98 版。

But now - EVERY TIME I run my tests this annoying text comes up:

但是现在 - 每次我运行测试时都会出现这个烦人的文本:

Chrome is being controlled by automated test software

Chrome 正在被自动化测试软件控制

And it asks me if I want to save password. (I can't add pictures because I don't have enough "points")

它问我是否要保存密码。(我无法添加图片,因为我没有足够的“积分”)

In the previous version, I had managed to block these 2 things by:

在以前的版本中,我设法通过以下方式阻止了这两件事:

public class CustomChromeDriver implements DriverSource {

    @Override
    public WebDriver newDriver() {
        try {
            DesiredCapabilities capabilities = DesiredCapabilities.chrome();
            Proxy proxy = new Proxy();
            String proxyServer = String.format("AProxyIDontWantToDisplay", System.getenv("proxy.username"), System.getenv("proxy.password"));
            proxy.setHttpProxy(proxyServer);
            capabilities.setCapability("proxy", proxy);
            ChromeOptions options = new ChromeOptions();
            options.addArguments(Arrays.asList("--no-sandbox","--ignore-certificate-errors","--homepage=about:blank","--no-first-run"));
            capabilities.setCapability(ChromeOptions.CAPABILITY, options);
            ChromeDriver driver = new ChromeDriver(capabilities);
            return driver;
        } catch (Exception e) {
            throw new Error(e);
        }
    }

    @Override
    public boolean takesScreenshots() {
        return true;
    }
}

I know there is this one (A link to same issue), but there are too many answers that don't work.

我知道有这个(指向同一问题的链接),但是有太多不起作用的答案。

Anybody that knows how to remove that?

有谁知道怎么去掉吗?

回答by Anuj Teotia

Previously, passing the "disable-infobars” ChromeOption to the WebDriver prevented Chrome from displaying this notification. Recently, the "disable-infobars" option has been deprecated and no longer removes the notification. The current workaround for this is to pass in an option called "excludeSwitches" and then exclude the "enable_automation" switch.

以前,将“disable-infobars”ChromeOption 传递给 WebDriver 会阻止 Chrome 显示此通知。最近,“disable-infobars”选项已被弃用且不再删除通知。当前的解决方法是传入一个选项称为“excludeSwitches”,然后排除“enable_automation”开关。

ChromeOptions options = new ChromeOptions(); 
options.setExperimentalOption("excludeSwitches", new String[]{"enable-automation"}); 
WebDriver driver = new ChromeDriver(options); 

回答by jgode

Add this to the options you pass to the driver:

将此添加到您传递给驱动程序的选项中:

options.addArguments("disable-infobars");

回答by Sasank Sarma

Map<String, Object> prefs = new HashMap<String, Object>();
//To Turns off multiple download warning
prefs.put("profile.default_content_settings.popups", 0);

prefs.put( "profile.content_settings.pattern_pairs.*.multiple-automatic-downloads", 1 );

//Turns off download prompt
prefs.put("download.prompt_for_download", false);
                    prefs.put("credentials_enable_service", false);
//To Stop Save password propmts
prefs.put("password_manager_enabled", false);

ChromeOptions options = new ChromeOptions();
options.addArguments("chrome.switches","--disable-extensions");
//To Disable any browser notifications
options.addArguments("--disable-notifications");
//To disable yellow strip info bar which prompts info messages
options.addArguments("disable-infobars");

options.setExperimentalOption("prefs", prefs);
System.setProperty("webdriver.chrome.driver", "Chromedriver path");
options.addArguments("--test-type");
driver = new ChromeDriver(options);
Log.info("Chrome browser started");

回答by Mesut GUNES

May someone needs this for Capybara, Watir should be like this:

可能有人需要这个给 Capybara,Watir 应该是这样的:

Capybara.register_driver :chrome do |app|
  $driver = Capybara::Selenium::Driver.new(app, {:browser => :chrome, :args => [ "--disable-infobars" ]})
end

回答by PDD

If anyone is using Rails 5.1+, which changed the testing structure a bit and Capybara is configured in this file now for system tests:

如果有人使用Rails 5.1+,它稍微改变了测试结构,现在在这个文件中配置了Capybara用于系统测试:

application_system_test_case.rb

You can add "args"in the options for driven_bylike this:

您可以像这样在driven_by的选项中添加“args”

driven_by :selenium, using: :chrome, screen_size: [1400, 1400], options: { args: ["--disable-infobars"] }

回答by Tree Go

It works for me by using addArguments(array("disable-infobars"))

它通过使用addArguments(array("disable-infobars"))

This is for facebook/php-webdriver

这是用于facebook/php-webdriver

$options = new ChromeOptions();
$options->addArguments(array("disable-infobars"));
$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(ChromeOptions::CAPABILITY, $options);
$this->driver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $capabilities);

回答by cwnewhouse

While the disable-infobarsroute will work, it will likely suppress the infobar in all cases (as suggested here), not just the case that the OP is referring to. This is overkill at best, and could lead to unexpected and inexplicable behavior in the future if you are not getting some important message.

虽然disable-infobars路线会工作,它可能会抑制在所有情况下的信息栏(如建议在这里),而不仅仅是在OP指的是这种情况。这充其量是矫枉过正,如果您没有收到一些重要信息,可能会导致将来出现意外和莫名其妙的行为。

I think it's better to leverage the provided enable-automationswitch by disabling itin the excludeSwitchesarea of your config/setup while doing nothing with regards to disable-inforbars. The enable-automationswitch's description:

我认为最好enable-automation通过在配置/设置区域禁用它来利用提供的开关,excludeSwitches同时对disable-inforbars. 该enable-automation开关的说明:

Inform users that their browser is being controlled by an automated test.

通知用户他们的浏览器正在由自动化测试控制。

For nightwatch.conf.jsit would look something like this (and worked for me):

因为nightwatch.conf.js它看起来像这样(并且对我有用):

desiredCapabilities: {
  ...
  chromeOptions: {
    excludeSwitches: ['enable-automation'],
    ...
  }
}

This should only do what we are after: getting rid of that specific pesky message!

这应该只做我们所追求的:摆脱那个特定的讨厌的信息!

Edit [2017-11-14]: This is now causing an even more annoying Disable Developer Mode Extensionsalert/warning. I've tried every relevant-looking flag/switch I could find that might help, but to no avail. I've filed a bug with Chromium, so we'll see and I'll try to swing back through here if I get a resolution.

编辑 [2017-11-14]:这现在导致更烦人的Disable Developer Mode Extensions警报/警告。我已经尝试了我能找到的所有相关的标志/开关,但无济于事。我已经向 Chromium 提交了一个错误,所以我们会看到,如果我得到解决方案,我会尝试回到这里。

回答by MatthewThomasGB

Protractor solution:

量角器解决方案:

I arrived here searching for a Protractorsolution, if useful for anyone I found, with help from the above answers; with Protractor you can add Chrome specific options to the chromeOptions object, within the capabilities object in the protractor.config file, for example to use the disable-infobars option discussed above use the following:

我来到这里寻找Protractor解决方案,如果对我找到的任何人有用,在上述答案的帮助下;使用 Protractor,您可以在 protractor.config 文件中的功能对象中向 chromeOptions 对象添加 Chrome 特定选项,例如要使用上面讨论的 disable-infobars 选项,请使用以下内容:

capabilities: {
  'browserName': 'chrome',
  'chromeOptions': {
    'args': ['disable-infobars']
  }
},

To use the enable-automation also discussed above:

要使用上面也讨论过的启用自动化:

capabilities: {
  'browserName': 'chrome',
  'chromeOptions': {
    'excludeSwitches': ['enable-automation']
  }
}

disable-infobars is preferred in my circumstances.

在我的情况下,禁用信息栏是首选。

回答by Rishi12

public WebDriver SetupChromeDriver(){
        //Initialize Chrome Driver
        ChromeOptions options = new ChromeOptions();
        Map<String, Object> prefs = new HashMap<String, Object>();
        prefs.put("safebrowsing.enabled", "true"); 
        options.setExperimentalOption("prefs", prefs); 
        options.addArguments("--disable-notifications");
        options.addArguments("--start-maximized");
        options.addArguments("disable-infobars");
        System.setProperty("webdriver.chrome.driver", "E:/Importent Softwares/Chrome Driver/chromedriver_2.37.exe");
        driver = new ChromeDriver(options);
        return driver;
}

回答by Rajeev

ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("useAutomationExtension", false);
options.setExperimentalOption("excludeSwitches",Collections.singletonList("enable-automation"));    
WebDriver driver = new ChromeDriver(options);

Use the above codes for latest Chrome drivers.

将上述代码用于最新的 Chrome 驱动程序。