selenium FAQ:怎么样调用Javascript?

selenium自动化开发中,难免需要用到直接调用javascript,怎么用呢?

1
2
3
WebDriver driver; // Assigned elsewhere
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("return document.title");

直接将driver强制转化为JavascriptExecutor,然后执行javascript即可。