|
本帖最后由 aperson 于 2014-9-25 10:31 编辑
以下是iframe.htm的源码:- <html>
- <head>
- <title>iFrame Test</title>
- </head>
- <body>
- <div style="height: 300px;width: 400px;">
- <input type="text" name="txt" />
- <p>
- <iframe frameborder="yes" width="100%" style="height:100%;display: block;">
- <html>
- <body>
- <br>
- </body>
- </html>
- </iframe>
- <p>
- <input type="button" name="sbmt" value="SUBMIT" onclick="javascript:document.getElementsByName('txt')[0].value='You are welcome!'" />
- </div>
- </body>
- </html>
复制代码 以下是WebDriver的java版源码:- import java.util.concurrent.TimeUnit;
- import org.openqa.selenium.By;
- import org.openqa.selenium.JavascriptExecutor;
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.firefox.FirefoxDriver;
- public class Test1 {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- WebDriver driver = new FirefoxDriver();
- driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
- driver.manage().window().maximize();
- driver.navigate().to("http://localhost/webtest/iframe.htm");
- driver.findElement(By.name("txt")).sendKeys("hello");
- driver.switchTo().frame(driver.findElement(By.tagName("iframe")));
- JavascriptExecutor js = (JavascriptExecutor)driver;
- js.executeScript("document.write('hello everybody!')", driver.findElement(By.tagName("body")));
- js.executeScript("arguments[0].innerHTML = 'hello everybody!'", driver.findElement(By.tagName("body")));
- driver.switchTo().defaultContent();
- driver.findElement(By.name("sbmt")).click();
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {}
- driver.quit();
- }
- }
复制代码 只执行document.write就会导致无法switchTo.defaultContent,而且这时候即使输出driver.getWindowHandle都是失败的,而只执行arguments[0].innerHTML就没有问题。
貌似一旦执行document.write后,selenium core中的js就丢掉原来的document对象了。
谁能知道其中原因。
|
|