首先,您必须将driver切换到应该执行javascript的选项卡。我用这个:
public void goto2() throws InterruptedException {
ArrayList<String> winHandles = new ArrayList<String> (driver.getWindowHandles());
// it takes some ms to get the WindowHandles, so this solution does not avoid Thread.sleep completely
Thread.sleep(500);
driver.switchTo().window(winHandles.get(1));
}
另一件事是javascript本身。要等待页面完全加载,我使用以下命令:
public void waitDom() {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.onload = null;");
}