1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<
HTML
>
<
HEAD
>
<
TITLE
>寸木的Selenium+Ajax测试</
TITLE
>
<
SCRIPT
language
=
"javascript"
>
function loadFrame() {
// 取得DIV对象
var divElement = document.getElementById("TestDiv");
// 插入iFrame元素
divElement.innerHTML = "<
iframe
id
=
'TestFrame'
style
=
'width:200px;height:250px;'
/>";
// 取得动态插入的iFrame元素
var frameElement = document.getElementById("TestFrame");
frameElement.src = "SubPage.html";
}
</
SCRIPT
>
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=UTF-8"
/>
</
HEAD
>
<
BODY
>
<
H1
>主页面</
H1
>
下面的区域是测试用的Frame。
<
div
id
=
"TestDiv"
style
=
"width:200px;height:250px;border:solid 1px #000000;"
> </
div
><
br
/>
<
input
type
=
"button"
value
=
"Load Frame"
onclick
=
"loadFrame();"
id
=
"btnLoad"
/><
br
/>
接收到的信息:<
label
id
=
"lblMsg"
>Waiting mesage</
label
>
</
BODY
>
</
HTML
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<
HTML
>
<
HEAD
>
<
TITLE
>寸木的Selenium+Ajax测试</
TITLE
>
<
SCRIPT
language
=
"javascript"
>
function removeDefaultInfo(webElement) {
var strDefalutInfo = "请输入信息...";
if(webElement.value == strDefalutInfo) {
webElement.value = "";
}
}
function setDefaultInfo(webElement) {
var strDefalutInfo = "请输入信息...";
if(webElement.value == "") {
webElement.value = strDefalutInfo;
}
}
function sendToMainPage() {
parent.document.getElementById("lblMsg").innerHTML = document.getElementById("frameText").value;
}
</
SCRIPT
>
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=UTF-8"
/>
</
HEAD
>
<
BODY
style
=
"background-color:#009900;"
>
<
H1
>子页面</
H1
>
Hi, 这里是子页面,为了和主页面加以区别,我被标注成了绿色。<
br
/ >
<
input
type
=
"text"
id
=
"frameText"
value
=
"请输入信息..."
onfocus
=
"removeDefaultInfo(this);"
onblur
=
"setDefaultInfo(this);"
/>
<
input
type
=
"button"
value
=
"传送到主窗口"
onclick
=
"sendToMainPage();"
id
=
"btnSendBack"
/>
</
BODY
>
</
HTML
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
/**
*
*/
package
com.cnblogs.www.hexin0614;
import
org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.firefox.FirefoxDriver;
/**
* @author hexin
*
*/
public
class
TestSa {
/**
* @param args
* @throws Exception
*/
public
static
void
main(String[] args)
throws
Exception {
// Declare
WebDriver driver =
new
FirefoxDriver();
// Load page
driver.get(
"file:///C:/tmp/Main.html"
);
Thread.sleep(
15000
);
// Load subpage
driver.findElement(By.id(
"btnLoad"
)).click();
// Input message
driver.findElement(By.id(
"frameText"
)).sendKeys(
"Message from Selenium."
);
// Send message back to main page
driver.findElement(By.id(
"btnSendBack"
)).click();
Thread.sleep(
2000
);
// Go back to main frame
System.out.println(driver.findElement(By.id(
"lblMsg"
)).getText());
driver.close();
}
}
|
1
2
|
// Step into the subpage
driver.switchTo().frame(
"TestFrame"
);
|
1
|
Return an opaque handle to this window that uniquely identifies it within this driver instance.This can be used to switch to this window at a later date
|
1
2
3
4
5
6
7
|
// Back up main page's handler
String strMainHandler = driver.getWindowHandle();
// ........
// Go back to main frame
driver.switchTo().window(strMainHandler);
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
/*
* Test
*/
package com.cnblogs.www.hexin0614;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
/**
* @author hexin
*
*/
public class TestSa {
/**
* @param args
* @throws Exception
*/
public
static
void
main(String[] args)
throws
Exception {
// Declare
WebDriver driver =
new
FirefoxDriver();
// Load page
driver.get(
"file:///C:/tmp/Main.html"
);
Thread.sleep(
15000
);
// Load subpage
driver.findElement(By.id(
"btnLoad"
)).click();
// Back up main page's handler
String strMainHandler = driver.getWindowHandle();
// Step into the subpage
driver.switchTo().frame(
"TestFrame"
);
// Input message
driver.findElement(By.id(
"frameText"
)).sendKeys(
"Message from Selenium."
);
// Send message back to main page
driver.findElement(By.id(
"btnSendBack"
)).click();
Thread.sleep(
2000
);
// Go back to main frame
driver.switchTo().window(strMainHandler);
System.out.println(driver.findElement(By.id(
"lblMsg"
)).getText());
driver.close();
}
}
|