Is it possible to automatically open Google Chrome

2019-04-04 15:25发布

问题:

Given that Selenium 2 closes the devtools window which contains my emulator profile saved under my user profile for chrome. Is there a way to trigger devtools to open using a selenium script?

Here is the info on the devtools window closing issue https://sites.google.com/a/chromium.org/chromedriver/help/devtools-window-keeps-closing

I feel a little exhausted trying some of these Chromium override parameters only one of which seems to work http://peter.sh/experiments/chromium-command-line-switches/

The one that had any affect is the following

options.addArguments("user-data-dir=/Path/to/chrome/profile");

If there is no way to open the dev tools window or panel, is there a way to initialize the emulator?

回答1:

at the terminal, type the following -

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --auto-open-devtools-for-tabs


回答2:

You can use Robot class for same. It just going to help you to open dev-tools on any browser.

Use this code on the place you want to open dev-tools

    try{
        Robot robot=new Robot();
        robot.keyPress(KeyEvent.VK_F12);
        robot.keyRelease(KeyEvent.VK_F12);  
    }
    catch(Exception ex){
        System.out.println(ex.getMessage());
    }


回答3:

You can used below given code, it is working fine with FireFox just change the browser

public void open(String url) {
    driver.get(url);
    Actions action = new Actions(driver);
    String pressF12=Keys.chord(Keys.F12,"");
    driver.findElement(By.id("lst-ib")).sendKeys(pressF12);
}


回答4:

As the same link you provided says, it's not possible to use ChromeDev tools with Selenium's ChromeDriver since 2.x. It's simply in direct conflict, you could use either one or the other.

The only way to perform a workaround is to pause all chrome driver interactivity, launch the DevTool's through some other automation framework such as Robots API, do whatever you need, and continue.

But I might think on what I need from dev tools, isn't there an alternative to what you need by providing Chrome with the proper configuration on launch? (loading a different profile, or loading an emulated device)



回答5:

Why do you need the chrome devtools anyway?

  • Is it for monitoring the network traffic? Why not usefirefoxdriver with firebug.xpi or browsermobproxy.
  • To open a page in emulated device's browser. This can be done with chrome, without opening the devtools.

WebDriver with chrome emulated browser

This is a code snippet that you can use to fire up chrome browser in a particular emulate version. Note the key thing here is the deviceName value, which should match as it is mentioned in the chrome browser of yours.

public static WebDriver MobileOpen()
{
    Map<String, String> mobileEmulation = new HashMap<String, String>();
    System.setProperty("webdriver.chrome.driver","path/to/chromedriver");
    mobileEmulation.put("deviceName", "Google Nexus 6");
    Map<String, Object> chromeOptions = new HashMap<String, Object>();
    chromeOptions.put("mobileEmulation", mobileEmulation);

    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);

    WebDriver driver = new ChromeDriver(capabilities);
    return driver;
}

Hope this helps.
Edit: I just saw it is a 3 years old post. The guy probably has found his way home. :P



回答6:

Have you tried the console API for your needs? It will allow you to invoke some dev-tool functions from inside JS.