2oO`[+fT9o1B?&h01. 文本框Textbox51Testing软件测试网"Nhx&l\
/?8A4vNN.S0 // Find the username input element by its
name51Testing软件测试网8b,VB uJd
WebElement username =
driver.findElement(By.name("username"));
:`+t
nC5gM051Testing软件测试网 l9PfL{jfq
m // Enter the user
name: jill
.^'O_Tq+fmh4mni0 username.clear();51Testing软件测试网 B {5XHo$m
username.sendKeys("jill");51Testing软件测试网%jM&K,Bd&h
51Testing软件测试网(Y%q#u9z+N%f,g
E,T1cU
// Find the password input element by its
name51Testing软件测试网/s*{3S)b3N
~
WebElement password =
driver.findElement(By.name("password"));51Testing软件测试网jP;z,M
k~
b/bl$BZxY0 // Enter the
password:123456
l0PH0n6}v3j0 password.clear();51Testing软件测试网F4oc
f(Ll
password.sendKeys("123456");51Testing软件测试网x7Qh!iM5C(pU/b
51Testing软件测试网\}d)?TR2e"h2.
按钮Button
j?ab8W.|#q1T0$`{3^@b@irQ3on+AK0 // Click the Login button by
className51Testing软件测试网 PF,}P Y-Tdz?1i1E
WebElement loginButton =
driver.findElement(By.className("btnTile")); 51Testing软件测试网@;mq*O[ `!N^:O2C;L^7i
loginButton.click();51Testing软件测试网Qj;mmk
51Testing软件测试网3~"O
D4W,a'^)?e@A"?3. 下拉框Select
.[2XY#S rzW:{ r051Testing软件测试网2OBCG#l8d //通过id或者name来定位下拉框,
new一个Select对象
.v4x@ig_051Testing软件测试网as/@(U
rO:Rle Select globalSelect = new
Select(driver.findElement(By.id("selectid")));
;@#A&} zFM;o*f051Testing软件测试网#jdwt:UL"| //通过选项索引选中第二项51Testing软件测试网4d"|6xr%\m
51Testing软件测试网Ig'z;b@-]'E]*@ globalSelect.selectByIndex(1);--[注]index是从0开始的
i gU _ b0A?B?&X6Y4Gt0 //通过下拉选项的内容选中第二项(second_item)51Testing软件测试网q1z't}+S
51Testing软件测试网jtqe\O7KV globalSelect.selectByVisibleText("second_item");51Testing软件测试网~7fi
['dlDB
(eV;Pv,C;V0 //遍历Select下拉列表的选项
Wh]
`YD-@(\
m K0_/[^SYh6Bu0 WebElement select =
driver.findElement(By.tagName("select"));51Testing软件测试网&lF.iN(^h3kg
51Testing软件测试网v1SZ:[? List<WebElement> myOptions =
select.findElements(By.tagName("option"));51Testing软件测试网lVa#{5Qw&w
51Testing软件测试网V{[P k1K for (WebElement option : myOptions) {51Testing软件测试网XA4b:v3U
8R&NpHf:e0
System.out.println(option.getAttribute("value");
{"L$]"Zpg051Testing软件测试网
mE$WS,q option.click();
.n6T7u^f$r@%Hz0|9?GU
wJ IS7z0 }
Brh"z]@3M^,Q0_fDFJ+I
x:A04. 对话框Alert, Confirm,
Prompt
$Gu&N"it%]B S
MM0/ua.m({A[I0 //捕获Alert/Confirm/Prompt对话框51Testing软件测试网 D O#[3|t8^B
51Testing软件测试网)i
Oyl/~.A/\'Kd Alert alert = driver.switchTo().alert();
G3R^A(E;H&JQ0_p/v.A$C`kl8x0 //获得Alert/Confirm/Prompt对话框内容
n\%z rL(_"S
wY0wCLmdv |5jv,@0 String alertText =
alert.getText();
2LsUE*IZ051Testing软件测试网?h"D:q*gRrUx#T //确认Alert/Confirm/Prompt
T/p Z,gX0 alert.accept();51Testing软件测试网-Yah k;E$QI'u
51Testing软件测试网j||N A` //取消Alert/Confirm/Prompt
XxSIoV3r.n8s051Testing软件测试网9~T8ydF.mml alert.dismiss();51Testing软件测试网+kibf|G2B
51Testing软件测试网pM'k2]7v(v3Y2_y //输入值到Prompt
t9lI#z3cP8@07|d7J7J4H9d8FpqT9P0 prompt.sendKeys("abc");51Testing软件测试网1r5JAy9f5i)q%v+z9g
!lM pa,ck1I5Z0 51Testing软件测试网 `"f(j)oaES;J+Z
51Testing软件测试网^F9L-Da*m5. 弹出窗口 popup window
z.NS.^$^y051Testing软件测试网HW \nt:Z8`.U //使用driver.switchTo()可以进行Windows, Frames,
Alerts, Confirms, Prompts之间的切换51Testing软件测试网5dt%l1H)d
51Testing软件测试网/j,W{){6B.a(\g+S driver.switchTo().window("windowhandle");
| C2m"Mb051Testing软件测试网v!AN;^l/w2\H driver.switchTo().frame("framename");51Testing软件测试网b p3Wr{;iC$v
"O#l/b+@6saL5S%h8ID0 driver.switchTo().alert();
1nbr!x6uth051Testing软件测试网KSsps@d
M3s%r
Dc
\?0@
Nl,F'Z8g*t06.
切换到新的Window(import
java.util.Set;)51Testing软件测试网2o ]9K;f/V%l'b,H ~&B
51Testing软件测试网'd&s*lZoCv(] //1. 保存之前的Windows Handles51Testing软件测试网p?%~
c3F}-Ni
51Testing软件测试网:F5\/l/GN'F%I Set<String> beforeHandlers =
driver.getWindowHandles();
B\!O1pM$L;z0 String beforeHandler =
beforeHandlers.iterator().next();51Testing软件测试网;KOl+X6s&C;LZs
51Testing软件测试网4\A/Y(jM:O%H+w // 2. 执行弹出新窗口的操作51Testing软件测试网O7`'wEbK
P
WebElement KButton =
driver.findElement(By.name("OK"));51Testing软件测试网)c+]P+xEd8^,P
OKButton.click();
|Q6Wz |Go0g4~@/Iaa0 //3. 保存现在的Windows
Handles
ch^;th0 Set<String>
afterHandlers = driver.getWindowHandles();51Testing软件测试网(BBr^)lSt
51Testing软件测试网 {v-j4{ z%Y@
//4. 从现在的Windows
Handles中remove调之前的,就能得到刚弹出新窗口的window handle
q-L``Rd051Testing软件测试网+P$W$_1y(`4R|$l afterHandlers.removeAll(beforeHandlers);51Testing软件测试网9@{|9Q}}6wr
]cIO"m4ZO0 String
newWinhandler =
afterHandlers.iterator().next();51Testing软件测试网/gp ^i[/]
51Testing软件测试网
[ R/RYP"C
//5.
切换到新弹出的窗口
:y.P6G {!z-rd(?}WX-U0 driver.switchTo().window(newWinhandler);
t;}4zo8f+X051Testing软件测试网iwL#ph7G //6. 返回之前的窗口
c)t5awu051Testing软件测试网5\6GfE ICY$C%XU
driver.switchTo().window(beforeHandler);51Testing软件测试网-S+aM&Xp!kb!x
&QZ"K,[;{.n0
OLD3b~B?,m0 d%i.Y'{2lWPS7E*EO*K07. 上传文件
{\)@SAXG051Testing软件测试网+|n
b0l0i1hY WebElement FileUpload
=driver.findElement(By.name("fileupload"));51Testing软件测试网 hl `FA)C
String filePath =
"C:\\myFile01";
vH'h ~b f0 earFileUpload.sendKeys(filePath);51Testing软件测试网!J o&q)}q&S$W-{