2

Below is my scenario :

  1. Open the URL (http://google.com)
  2. Press "F12" key

I have tried below lines of code :

public static void main(String[] args) throws InterruptedException {

    WebDriver driver=new FirefoxDriver();
    driver.manage().window().maximize();
    driver.get("http://google.com");

    String CurrentURL= driver.getCurrentUrl();
    System.out.println("Current URL is : " + CurrentURL);

    Actions action = new Actions(driver);
    action.sendKeys(Keys.F12);

    System.out.println("successfuly pressed key F12");
    driver.close();
}

It is printing "successfuly pressed key F12" on the console. But, I don't see 'F12' being pressed on website.

Please can anyone help me out of this ?

Thanks in advance.

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
naazneen3264
  • 315
  • 2
  • 7
  • 22
  • 1
    "successfuly pressed key F12" will be printed whether or not the `action.sendKeys()` call manages to do it or not (suceeds or fails) – Shark Feb 01 '16 at 14:18
  • Also, what do you expect to happen on the site? You're closing the browser (almost) immediately after, without any waiting. – Andrew Regan Feb 01 '16 at 14:36
  • F12 opens the dev console. I haven't tried this but you might be able to check the viewport size before and after F12 is pressed and make sure there is a size difference to detect whether the dev console opened or not. I'm not sure there's another way to determine if it opened. – JeffC Feb 01 '16 at 17:11

5 Answers5

3

I have been trying to use C# selenium to automatically open browsers with the devtools console open. To date (January 2020) My experience with C# is Chrome options.AddArguments("--auto-open-devtools-for-tabs"); Firefox options.AddArgument("-devtools"); IE11 no command line options but you can use driver.FindElement(By.Id("body")).SendKeys(Keys.F12); after the browsers is open Edge I have not been able to find any way to do this automatically, so selecting the browser and pressing F12 will have to suffice.

Thanks to the other contributors who helped me get this far

Patrick Pope
  • 31
  • 1
  • 1
2

For pressing F12: The following Selenium Java code using Robot could work both in Firefox and Chrome:

driver.get("https://www.google.com/");
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_F12);
robot.keyRelease(KeyEvent.VK_F12);
Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
2

VS 2017 , Selenium v 3.12.1 ,C# , Firefox V 60.0.2 , Chrome V 66 , Nunit v3.10.1 , Gecko Driver v 20.1 , chrome driver v 2.4

I tried to search for Firefox but did not success but I do get solution for Chrome v66

Please provide profile like this:

options.AddArguments("--auto-open-devtools-for-tabs");
    

This is the 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));
    
    

you may have a look here as well: https://peter.sh/experiments/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();
alelom
  • 2,130
  • 3
  • 26
  • 38
Mike ASP
  • 2,013
  • 2
  • 17
  • 24
0

Can you try pressing F12 on body of the website? I used below java junit code and it opened google and pressed F12.

@Test
public void Test_Google_FireFox() throws Exception {
    driver = new FirefoxDriver();
    driver.manage().window().maximize();
    baseUrl = "https://www.google.com";
    driver.get(baseUrl);
    driver.findElement(By.xpath("/html/body")).sendKeys(Keys.F12);

OR,

driver.findElement(By.cssSelector("body")).sendKeys(Keys.F12);

OR,

driver.findElement(By.tagName("body")).sendKeys(Keys.F12);

}
Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Abdul Hameed
  • 1,135
  • 1
  • 13
  • 24
0

I think you forgot to add the perform method. So it should be:

Actions action = new Actions(driver);
action.sendKeys(Keys.F12);
action.perform();

or

Actions action = new Actions(driver);
action.sendKeys(Keys.F12).perform();