如何启动浏览器已在上篇文章中说明,这里还是以chrome为例,firefox、IE启动方式相同。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
//启动浏览器
import
org
.
openqa
.
selenium
.
WebDriver
;
import
org
.
openqa
.
selenium
.
chrome
.
ChromeDriver
;
/*启动IE需要的包
import org.openqa.selenium.ie.InternetExplorerDriver;
启动firefox需要的包
import org.openqa.selenium.firefox.FirefoxDriver;*/
public
class
NewTest
{
public
static
void
main
(
String
[
]
args
)
{
//设置chromedriver地址
System
.
setProperty
(
"webdriver.chrome.driver"
,
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"
)
;
//driver即为浏览器对象
WebDriver
driver
=
new
ChromeDriver
(
)
;
}
}
|
浏览器的操作方法可以在WebDriver.class中找到接口说明,本文主要关注Navigation和Window接口中的方法。
maximize方法无需参数也没有返回值,直接使用即可
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import
org
.
openqa
.
selenium
.
WebDriver
;
import
org
.
openqa
.
selenium
.
chrome
.
ChromeDriver
;
public
class
NewTest
{
public
static
void
main
(
String
[
]
args
)
{
System
.
setProperty
(
"webdriver.chrome.driver"
,
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"
)
;
WebDriver
driver
=
new
ChromeDriver
(
)
;
//最大化浏览器
driver
.
manage
(
)
.
window
(
)
.
maximize
(
)
;
}
}
|
设置浏览器的大小有两个比较常见的用途:
1.多浏览器操作时方便在屏幕上查看
2.方便测试不同浏览器大小下样式的展示,例如移动端页面的显示可以将浏览器大小设置为350*600
设置浏览器大小使用setSize方法,需要Dimension对象作为参数,获取当前浏览器窗口的大小使用getSize方法,返回 Dimension对象。Dimension位于org.openqa.selenium包中并提供了getHeight和getWidth方法输出高 宽。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
//设置和获取浏览器大小
import
org
.
openqa
.
selenium
.
WebDriver
;
import
org
.
openqa
.
selenium
.
chrome
.
ChromeDriver
;
//导入Dimension
import
org
.
openqa
.
selenium
.
Dimension
;
public
class
NewTest
{
public
static
void
main
(
String
[
]
args
)
{
Dimension
dimensio
;
Dimension
browserSize
=
new
Dimension
(
350
,
600
)
;
System
.
setProperty
(
"webdriver.chrome.driver"
,
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"
)
;
WebDriver
driver
=
new
ChromeDriver
(
)
;
//设置浏览器大小
driver
.
manage
(
)
.
window
(
)
.
setSize
(
browserSize
)
;
//获取当前浏览器大小
dimensio
=
driver
.
manage
(
)
.
window
(
)
.
getSize
(
)
;
//输出
System
.
out
.
print
(
"浏览器当前高度为"
+
dimensio
.
getHeight
(
)
+
";浏览器当前宽度为"
+
dimensio
.
getWidth
(
)
)
;
}
}
|
两种方式可以访问链接,get方法和navigate().to方法,前者较常用。获取当前页面的url和title在测试重定向的页面时有很大的作用。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
import
org
.
openqa
.
selenium
.
WebDriver
;
import
org
.
openqa
.
selenium
.
chrome
.
ChromeDriver
;
public
class
NewTest
{
public
static
void
main
(
String
[
]
args
)
{
System
.
setProperty
(
"webdriver.chrome.driver"
,
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"
)
;
WebDriver
driver
=
new
ChromeDriver
(
)
;
driver
.
get
(
"http://anjuke.com"
)
;
System
.
out
.
println
(
"URL="
+
driver
.
getCurrentUrl
(
)
+
";title="
+
driver
.
getTitle
(
)
)
;
driver
.
navigate
(
)
.
to
(
"http://www.baidu.com"
)
;
System
.
out
.
println
(
"URL="
+
driver
.
getCurrentUrl
(
)
+
";title="
+
driver
.
getTitle
(
)
)
;
}
}
|
注意get和navigate().to方法用的是HTTP GET请求,输入的连接中必须加上http://前缀。
这三个功能并不常用,了解就行了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
import
org
.
openqa
.
selenium
.
WebDriver
;
import
org
.
openqa
.
selenium
.
chrome
.
ChromeDriver
;
public
class
NewTest
{
public
static
void
main
(
String
[
]
args
)
{
System
.
setProperty
(
"webdriver.chrome.driver"
,
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"
)
;
WebDriver
driver
=
new
ChromeDriver
(
)
;
driver
.
get
(
"http://anjuke.com"
)
;
driver
.
navigate
(
)
.
to
(
"http://www.baidu.com"
)
;
//后退到anjuke.com
driver
.
navigate
(
)
.
back
(
)
;
//前进到baidu.com
driver
.
navigate
(
)
.
forward
(
)
;
//刷新当前页面
driver
.
navigate
(
)
.
refresh
(
)
;
}
}
|
浏览器窗口之间的切换是常用的功能,webdriver提供了switchTo方法实现窗口之间的切换,使用这个方法需要提供浏览器窗口的Handle,具体实现如下
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
|
import
java
.
util
.
Set
;
import
org
.
openqa
.
selenium
.
By
;
import
org
.
openqa
.
selenium
.
WebDriver
;
import
org
.
openqa
.
selenium
.
chrome
.
ChromeDriver
;
import
org
.
openqa
.
selenium
.
WebElement
;
public
class
NewTest
{
public
static
void
main
(
String
[
]
args
)
{
System
.
setProperty
(
"webdriver.chrome.driver"
,
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"
)
;
WebDriver
driver
=
new
ChromeDriver
(
)
;
driver
.
get
(
"http://baidu.com"
)
;
//获得输入框对象
WebElement
input
=
driver
.
findElement
(
By
.
xpath
(
"//input[@id='kw1']"
)
)
;
//搜索安居客
input
.
sendKeys
(
"anjuke"
)
;
//获得提交按钮对象
WebElement
button
=
driver
.
findElement
(
By
.
xpath
(
"//input[@id='su1']"
)
)
;
button
.
click
(
)
;
//这里必须设定一个暂停时间,百度搜索结果页面加载的速度没有程序执行的速度快
//等待2S以等页面加载完成
try
{
Thread
.
sleep
(
2000
)
;
}
catch
(
InterruptedException
e
)
{
System
.
out
.
print
(
e
.
toString
(
)
)
;
}
//获取第一个搜索结果
WebElement
a
=
driver
.
findElement
(
By
.
xpath
(
"//div[@id='1']/h3/a"
)
)
;
a
.
click
(
)
;
//获取当前浏览器窗口标识
String
currentHandle
=
driver
.
getWindowHandle
(
)
;
//获取所有浏览器窗口标识
Set
<String>
handles
=
driver
.
getWindowHandles
(
)
;
for
(
String
handle
:
handles
)
{
if
(
handle
.
equals
(
currentHandle
)
)
continue
;
else
driver
.
switchTo
(
)
.
window
(
handle
)
;
}
System
.
out
.
println
(
driver
.
getCurrentUrl
(
)
)
;
//清楚driver对象
driver
.
quit
(
)
;
}
}
|
很多时候我们需要通过新开窗口的名称来切换窗口,只需要把上面的代码稍做修改,将切换窗口的功能封装成一个函数即可,请看
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
54
55
56
|
import
java
.
util
.
Set
;
import
org
.
openqa
.
selenium
.
By
;
import
org
.
openqa
.
selenium
.
WebDriver
;
import
org
.
openqa
.
selenium
.
chrome
.
ChromeDriver
;
import
org
.
openqa
.
selenium
.
WebElement
;
public
class
NewTest
{
public
static
void
main
(
String
[
]
args
)
{
System
.
setProperty
(
"webdriver.chrome.driver"
,
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"
)
;
WebDriver
driver
=
new
ChromeDriver
(
)
;
driver
.
get
(
"http://baidu.com"
)
;
//获得输入框对象
WebElement
input
=
driver
.
findElement
(
By
.
xpath
(
"//input[@id='kw1']"
)
)
;
//搜索安居客
input
.
sendKeys
(
"anjuke"
)
;
//获得提交按钮对象
WebElement
button
=
driver
.
findElement
(
By
.
xpath
(
"//input[@id='su1']"
)
)
;
button
.
click
(
)
;
//这里必须设定一个暂停时间,百度搜索结果页面加载的速度没有程序执行的速度快
//等待2S以等页面加载完成
try
{
Thread
.
sleep
(
2000
)
;
}
catch
(
InterruptedException
e
)
{
System
.
out
.
print
(
e
.
toString
(
)
)
;
}
//获取第一个搜索结果
WebElement
a
=
driver
.
findElement
(
By
.
xpath
(
"//div[@id='1']/h3/a"
)
)
;
a
.
click
(
)
;
driver
=
switchWindow
(
driver
,
"上海二手房"
)
;
System
.
out
.
println
(
"成功切换到"
+
driver
.
getTitle
(
)
)
;
driver
.
quit
(
)
;
}
private
static
WebDriver
switchWindow
(
WebDriver
dr
,
String
winTitle
)
{
//获取当前浏览器窗口标识
String
currentHandle
=
dr
.
getWindowHandle
(
)
;
//获取所有浏览器窗口标识
Set
<String>
handles
=
dr
.
getWindowHandles
(
)
;
for
(
String
handle
:
handles
)
{
if
(
handle
.
equals
(
currentHandle
)
)
continue
;
else
{
dr
.
switchTo
(
)
.
window
(
handle
)
;
if
(
dr
.
getTitle
(
)
.
contains
(
winTitle
)
)
{
break
;
}
else
continue
;
}
}
return
dr
;
}
}
|
直接调用close方法即可,quit方法是用来退出driver的,每一次启动chrome都会启动一个chrome.drivre进程,需使用quit方法退出。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
import
org
.
openqa
.
selenium
.
WebDriver
;
import
org
.
openqa
.
selenium
.
chrome
.
ChromeDriver
;
public
class
NewTest
{
public
static
void
main
(
String
[
]
args
)
{
System
.
setProperty
(
"webdriver.chrome.driver"
,
"C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe"
)
;
WebDriver
driver
=
new
ChromeDriver
(
)
;
driver
.
get
(
"http://baidu.com"
)
;
driver
.
close
(
)
;
driver
.
quit
(
)
;
}
资料来源:http://zlshuo.com/selenium/1155.html |