有些时间脚本需要一些时间来加载列表所以需要添加等待,
WebElement ar=driver.findElement(By.id("id_attendees"));
Thread.sleep(1000);
ar.sendKeys(Keys.ARROW_DOWN);
我觉得你对Actions的使用不太对。
实现是构建器模式。调用 sendKeys 不会发送事件,它只会在您调用 perform 时暂存要触发的事件。请注意,sendKeys 的返回值是一个Actions 实例。
Actions actionObject = new Actions(driver);
actionObject = actionObject.sendKeys(Keys.ARROW_DOWN); //ASSIGN the return or you lose this event.
actionObject.perform(); //Should do what you want. Note that this will reset the builder.
希望有帮助。
对于 Actions 类,在定义了它将为您做什么之后,您需要首先构建()它。所以在你的情况下它会是这样的:
Actions actionObject = new Actions(driver);
actionObject.sendKeys(Keys.ARROW_DOWN).build();
当你想让你的脚本执行那个动作时,你需要执行()它。您可以在 build() 方法之后立即链接它(例如,如果您只使用它一次)或稍后在您的代码中需要它时,就像这样:
actionObject.sendKeys(Keys.ARROW_DOWN).build().perform();
或
actionObject.perform();
祝你好运!