今天有個(gè)朋友在群里問 WebElement.getText() 值為空,當(dāng)你發(fā)現(xiàn)取到的值為空的時(shí)候,會(huì)不會(huì)郁悶?zāi)兀棵髅骺吹降闹挡粸榭眨_本看著也沒有問題啊,為何取到的值為空呢!!!萬千糾結(jié)啊,若是長時(shí)間困在此處,是不是猶如修煉沖關(guān)遇到了瓶頸,就差那么一點(diǎn)點(diǎn)就可得道,哎。。。。。。
不知道其他的各位小主們有沒有遇到過這種情況,下面俺就針對(duì)此問題提出一些俺自己的見解,敬請(qǐng)各位小主參閱,若有不足或錯(cuò)誤之處,敬請(qǐng)大神告知,非常感謝!
出現(xiàn)此種情況的原因有以下幾點(diǎn):
1、元素隱藏,即 WebElement.isDisplayed = false,當(dāng) WebDriver 斷定 isDisplayed = false 時(shí),WebElement.getText() 值為空;
???? --> 解決方法:通過 js 修改元素的屬性,使其 CSS display 的值為 block,而非 none
2、元素的值存在某個(gè)屬性當(dāng)中,例如 value 屬性,此種一般是通過 js 操作賦值到 value 的
???? --> 解決方法:通過 WebElement.getAttribute('') 獲取對(duì)應(yīng)的值,當(dāng) isDisplayed = false 時(shí),WebElement.getAttribute('') 可正確獲取元素對(duì)應(yīng)的屬性值
3、當(dāng)前元素并未存儲(chǔ)相應(yīng)的操作值,操作值被存在了其他的地方
???? --> 解決方法:找到對(duì)應(yīng)的元素,獲取值
同時(shí),俺在網(wǎng)上找到了一個(gè)實(shí)例對(duì)應(yīng)上述三個(gè)原因的第 3 點(diǎn),因而就以易迅網(wǎng)搜索商品的搜索框?yàn)槔瑢?shí)際演示一下,以下為源碼,請(qǐng)參閱:
1
package
main.aaron.demo.javascript;
2
3
import
org.openqa.selenium.By;
4
import
org.openqa.selenium.JavascriptExecutor;
5
import
org.openqa.selenium.WebElement;
6
import
org.testng.annotations.Test;
7
8
import
main.aaron.sele.core.TestCase;
9
10
/**
11
* JavaScript 在 Selenium 自動(dòng)化中的應(yīng)用實(shí)例
12
*
13
*
@author
Aaron.ffp
14
*
@version
V1.0.0: autoSeleniumDemo main.aaron.demo.javascript JavaScript.java, 2015-6-17 00:24:59 Exp $
15
*/
16
public
class
JavaScript
extends
TestCase{
17
//
執(zhí)行 JavaScript 腳本,輸入查詢內(nèi)容
18
private
String jscript = "document.getElementById('q_show').value='iPhone 6 plus'"
;
19
private
String baseUrl = "http://www.yixun.com/"
;
20
21
/**
22
* JavaScript 賦值
23
*
24
*
@author
Aaron.ffp
25
*
@version
V1.0.0: autoSeleniumDemo main.aaron.demo.javascript JavaScript.java testJscript, 2015-6-17 12:37:57 Exp $
26
*
27
*
@throws
InterruptedException
28
*/
29
@Test
30
public
void
testJscript()
throws
InterruptedException{
31
//
打開易迅網(wǎng)首頁
32
this
.webdriver.get(
this
.baseUrl);
33
34
//
獲取搜索按鈕
35
WebElement search =
this
.webdriver.findElement(By.cssSelector(".mod_search_btn"
));
36
37
//
輸入查詢值
38
this
.execJavascript(jscript);
39
40
//
獲取搜索框
41
WebElement in =
this
.webdriver.findElement(By.id("q_show"
));
42
43
//
點(diǎn)擊搜索按鈕
44
search.click();
45
46
//
獲取搜索值的真正存儲(chǔ)位置
47
WebElement intext =
this
.webdriver.findElement(By.cssSelector("#keyWord"
));
48
49
//
頁面刷新,需要重新獲取元素,否則會(huì)報(bào)錯(cuò)
50
in =
this
.webdriver.findElement(By.id("q_show"
));
51
52
//
搜索框中的值未曾保存在 q_show 元素內(nèi),因而最終獲得的值為空
53
System.out.println("現(xiàn)像值 --- q_show.getText() : " +
in.getText());
54
//
搜索框中的值實(shí)際保存的元素,對(duì)應(yīng)值為空的第二種原因
55
System.out.println("本質(zhì)值,對(duì)應(yīng)值為空的第二種原因 --- #keyWord.getAttribute('') : " +
intext.getText());
56
//
搜索框中的值實(shí)際保存的元素,對(duì)應(yīng)值為空的第三種原因
57
System.out.println("本質(zhì)值,對(duì)應(yīng)值為空的第三種原因 --- #keyWord.getAttribute('') : " + intext.getAttribute("value"
));
58
59
this
.webdriver.close();
60
this
.webdriver.quit();
61
}
62
63
/**
64
* 執(zhí)行 JScript 腳本
65
*
66
*
@author
Aaron.ffp
67
*
@version
V1.0.0: autoUISelenium main.java.aaron.sele.core SeleniumCore.java execJavascript, 2015-6-17 00:25:38 Exp $
68
*
69
*
@param
jscript 腳本
70
*/
71
public
void
execJavascript(String jscript){
72
((JavascriptExecutor)
this
.webdriver).executeScript(jscript);
73
}
74
}
至此, WebUI 自動(dòng)化功能測(cè)試腳本 第 011 篇-WebElement.getText()值為空問題探索及解決 順利完結(jié),希望此文能夠給初學(xué) Selenium 的您一份參考。
最后,非常感謝親的駐足,希望此文能對(duì)親有所幫助。熱烈歡迎親一起探討,共同進(jìn)步。非常感謝! ^_^
Selenium2學(xué)習(xí)-013-WebUI自動(dòng)化實(shí)戰(zhàn)實(shí)例-011-WebElement.getText()值為空問題探索及解決
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

