driver.switchTo().frame("navigation"); // Selects the first frame on the page driver.switchTo().defaultContent(); System.out.println(driver.getPageSource()); driver.switchTo().frame("detail"); driver.switchTo().frame("header"); X
<head> <script type="text/javascript"> function display_alert() { alert("I am an alert box!!") } </script> </head> <body> <button id=“btn” onclick="display_alert()">test alert</button> </body>
//Open or Save Dialog Robot robot = new Robot(); //Alt + S to select save robot.keyPress(KeyEvent.VK_ALT); robot.keyPress(KeyEvent.VK_S); robot.keyRelease(KeyEvent.VK_ALT); robot.delay(3000); //Then type several chars robot.keyPress(KeyEvent.VK_T); robot.keyPress(KeyEvent.VK_E); robot.keyPress(KeyEvent.VK_S); robot.keyPress(KeyEvent.VK_T); //Then press enter robot.keyPress(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER);
/** * Get specific cell element from table * @param table * @param rowIndex - start from 0 * @param columnIndex - start from 0 * @return */ public static WebElement getCell(WebElement table, int rowIndex, int columnIndex) { String xpathStr = ".//tr[" + (rowIndex + 1) + "]/td[" + (columnIndex + 1) + "]"; return table.findElement(By.xpath(xpathStr)); }
//Able to execute Javascript directly using JavascriptExecutor public void JavascriptExecutorTest() { driver.get("http://www.baidu.com/"); ((JavascriptExecutor)driver).executeScript("document.getElementById(\"kw\").value = \"JavascriptExecutortest\"");
}
//Using JavaScript implement the functions which Webdriver API doesn’t provide.
/** * Get the inner HTML of the element by id * @param driver * @param id * @return */ public static String getInnerHTML(WebDriver driver, String id) { JavascriptExecutor js = (JavascriptExecutor) driver; return (String) js.executeScript("return document.getElementById(" + "\"" + id + "\"" + ").innerHTML"); }
//continue to this website(not recommended) driver.navigate().to("javascript:document.getElementById('overridelink').click()");
Appendix: Firefinder plugin for Firefox
Official Selenium Blog http://seleniumhq.wordpress.com/ Google Code - Selenium http://code.google.com/p/selenium/ Google Group - WebDriver http://groups.google.com/group/webdriver Google Group - Selenium Users http://groups.google.com/group/selenium-users LinkedIn – Selenium-WebDriver http://www.linkedin.com/groups/Selenium-WebDriver-4067187 XML Path Language (XPath) http://www.w3.org/TR/xpath/ Document Object Model (DOM) http://www.w3.org/DOM/DOMTR