直接通过Selenium按“F12”键

时间:2016-02-01 14:12:07

标签: java selenium selenium-webdriver

以下是我的情景:

  1. 打开网址(http://google.com
  2. 按“F12”键
  3. 我尝试了以下代码行:

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

    在控制台上打印“成功按键F12”。但是,我没有在网站上看到'F12'被按下。

    请有人帮我解决这个问题吗?

    提前致谢。

5 个答案:

答案 0 :(得分:1)

按F12 :使用Robot的以下Selenium Java代码可以在Firefox和Chrome中使用:

driver.get("https://www.google.com/");
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_F12);
robot.keyRelease(KeyEvent.VK_F12);

答案 1 :(得分:0)

您可以尝试在网站正文上按F12吗?我使用下面的java junit代码,它打开谷歌并按下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);

或,

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

}

答案 2 :(得分:0)

我想你忘了添加perform方法。 所以它应该是:

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

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

答案 3 :(得分:0)

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


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

答案 4 :(得分:0)

我一直在尝试使用C#硒在devtools控制台打开的情况下自动打开浏览器。到目前为止(2020年1月),我在C#方面的经验是 Chrome options.AddArguments("--auto-open-devtools-for-tabs"); Firefox options.AddArgument("-devtools"); IE11 没有命令行选项,但是您可以在打开浏览器后使用driver.FindElement(By.Id("body")).SendKeys(Keys.F12); Edge 我无法找到自动执行此操作的任何方法,因此选择浏览器并按F12键就足够了。

感谢其他帮助我走到这一步的贡献者