Selenium sendkeys ctrl + 5无效

时间:2015-12-09 19:43:16

标签: selenium

我有一个要求,其中

  • 我需要在IE中调用F12开发人员工具。
  • 导航到探查器标签。
  • 启动探查器

我正在使用IE 9.在下面的代码中,我尝试通过输入 ctrl 5 进行导航。它不起作用。

import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.HasInputDevices;
import org.openqa.selenium.interactions.Keyboard;
import org.testng.annotations.Test;
import org.openqa.selenium.remote.DesiredCapabilities;

public class InvokeIEbrowser {

    @Test
    public void IEdriver() {
        System.setProperty("webdriver.ie.driver",
            "D:\\2015\\softwares\\IEDriverServer_x64_2.48.0\\IEDriverServer.exe");
        DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();

        capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
        capabilities.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
        capabilities.setCapability("ACCEPT_SSL_CERTS", true);

        WebDriver driver = new InternetExplorerDriver();

        /*
        Keyboard keyboard = ((HasInputDevices) driver).getKeyboard();
        keyboard.sendKeys(Keys.F12);
        */

        /*
        driver.get("http://mail.yahoo.com");
        System.out.println(driver.getTitle());
        */

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //driver.FindElement(By.XPath("String")).SendKeys(Keys.NumberPad5);
        //keyboard.sendKeys(Keys.CONTROL);
        //getKeyboard().sendKeys(Keys.CONTROL, Keys.F5);

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

        //actionObject.sendKeys(Keys.CONTROL).sendKeys(Keys.NUMPAD5).keyUp(Keys.CONTROL).perform();

        actionObject.sendKeys(Keys.CONTROL.NUMPAD5).perform();
        System.out.println("done");
    }
}

2 个答案:

答案 0 :(得分:0)

您可以尝试使用Keys.chord()(更多关于它here):

driver.FindElement(By.XPath("String")).SendKeys(Keys.chord(Keys.CONTROL, Keys.NUMPAD5 ));

或使用Action类和unicode表示:

 Actions action = new Actions(); 
  action.keyDown(Keys.CONTROL).sendKeys(String.valueOf('\u0035')).perform();

您可以找到有关unicode表示here的更多参考资料。

答案 1 :(得分:0)

您是否只是想将热键发送到该页面?尝试扩展驱动程序......

public static void sendCtrl5(this IWebDriver Driver)
    {
        Driver.FindElement(By.TagName("body")).Click(); // make window active
        new Actions(Driver)
            .SendKeys(Keys.Control + Keys.NumberPad5)
            .Perform();
    }

使用:driver.sendCtrl5();