Selenium WebDriver keyDown

时间:2017-07-12 19:17:50

标签: java selenium keydown

我在Java中使用Selenium而keyDown(Keys.CONTROL)似乎没有做任何事情。 我想做的是:

以下是代码:

    System.setProperty("webdriver.gecko.driver", "C:\\Users\\user\\Desktop\\Selenium\\geckodriver.exe");

    WebDriver driver = new FirefoxDriver();

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    driver.manage().window().maximize();

    driver.navigate().to("https://demos.devexpress.com/aspxeditorsdemos/ListEditors/MultiSelect.aspx");

    driver.findElement(By.id("ContentHolder_lbSelectionMode_I")).click();
    driver.findElement(By.id("ContentHolder_lbSelectionMode_DDD_L_LBI1T0")).click();


    WebElement OneElement = driver.findElement(By.id("ContentHolder_lbFeatures_LBT"));
    List<WebElement> options = OneElement.findElements(By.tagName("td"));   

//一切正常,直到这个,我点击“Multiple”,现在我尝试使用keyDown选择几行:

    Actions builder = new Actions(driver);

    Action multipleSelect = builder.keyDown(Keys.SHIFT).click(options.get(2)).click(options.get(4)).click(options.get(6)).build();

    multipleSelect.perform();

    Thread.sleep(5000);

    driver.close();

我希望一次选择几行,但由于未按下keyDown,我最终只会选择一行。

1 个答案:

答案 0 :(得分:0)

对于多个部分,您可以这样做:

Select select = new Select(element);
Actions builder = new Actions(driver);
builder.keyDown(Keys.CONTROL)
.click(select.getOptions().get(2))
.click(select.getOptions().get(3))
.click(select.getOptions().get(4))
.keyUp(Keys.CONTROL);

builder.build().perform();