問題描述
我是 Java 和 Selenium 的新手,所以如果我的問題聽起來有點初級,我提前道歉.
我使用:
driverChrome.findElements(By.className("blabla"));
查找以blabla"作為其類名的元素,例如:
<span class="blabla" title="標題">...</span>
現(xiàn)在,如果我想通過其他屬性查找所有元素怎么辦?類似:
driverChrome.findElements(By.titleValue("the title"));
這是我目前用于執(zhí)行此任務的代碼:
列表spans = driverChrome.findElements(By.tagName("span"));for (WebElement we : spans) {if (we.getAttribute("title") != null) {if (we.getAttribute("title").equals("the title")) {...休息;}}}
但它并不快速且易于使用.
XPath
歸檔元素有很多方法1 絕對路徑
<html><身體><表格><輸入 id="demo"/></表格></div></身體><html>獲取輸入"標簽
xpath="/html/body/div/form/input"
2 相對路徑
<html><身體><表格><輸入 id="demo"/></表格></div></身體><html>獲取輸入"標簽
xpath="http://輸入"
3 索引
<html><身體><表格><輸入id="demo1"/><輸入id="demo2"></表格></div></身體><html>獲取輸入'demo2'
xpath="http://輸入[1]"
4 任意單個屬性
<html><身體><表格><輸入id="demo1"/><輸入 id="demo2" foo="bar"></表格></div></身體><html>獲取輸入'demo2'
xpath="http://input[@id='demo2']" (相當于By.id)
或者
xpath="http://input[@foo='bar']"
5 任意多個屬性
<html><身體><表格><輸入id="1" type="提交"/><輸入 id="2" foo="bar"/><input id="3" type="submit" foo="bar"/></表格></div></身體><html>獲取第三個輸入
xpath="http://input[@type='submit'][@foo='bar']"
或者
xpath="http://input[@type='submit' and @foo='bar']"
如果在這里使用 xpath="http://input[@type='submit' 或 @foo='bar']" 你會得到一個數(shù)組.您可以通過 driver.findElements(By.xpath(xpath)) (java) 獲取列表.否則你會得到第一個元素(如果你只使用 driver.findElement).因為所有 3 個輸入元素都滿足您的條件或",它給了您第一個.
6 包含屬性
<html><身體><表格><輸入id="1" type="提交"/><input id="2" foo="bar" daddy="dog"/><input id="3" type="submit" foo="bar"/></表格></div></身體><html>獲取第二個輸入
xpath="http://input[@daddy]"
因為只有第二個有屬性'daddy'
7 內(nèi)搜索
<html><身體><表格><input id="input1" daddy="dog"/><input id="input2" daddy="pig"/></表格></div><表格><input id="input3" daddy="dog"/><input id="input4" daddy="apple"/></表格></div></身體><html>獲取第二個div
xpath="http://div[.//input[@daddy='dog'] 和 .//input[@daddy='apple']]"
總的來說,這些都是我現(xiàn)在可以解決的.希望對您有所幫助.
I am very new at Java and Selenium so my apologies in advance if my question sounds a bit primary.
I use:
driverChrome.findElements(By.className("blabla"));
to find elements which have "blabla" as their className, for example:
<span class="blabla" title="the title">...</span>
Now, what if I want to find all elements by their other attributes? something like:
driverChrome.findElements(By.titleValue("the title"));
This is the code that I am currently using to do this task:
List<WebElement> spans = driverChrome.findElements(By.tagName("span"));
for (WebElement we : spans) {
if (we.getAttribute("title") != null) {
if (we.getAttribute("title").equals("the title")) {
...
break;
}
}
}
but it is not fast and easy to use.
解決方案 There are many methods while archiving an element by XPath
1 Absolutely path
<html>
<body>
<div>
<form>
<input id="demo"/>
</form>
</div>
</body>
<html>
To get the 'input' tag
xpath="/html/body/div/form/input"
2 Relative path
<html>
<body>
<div>
<form>
<input id="demo"/>
</form>
</div>
</body>
<html>
To get the 'input' tag
xpath="http://input"
3 Index
<html>
<body>
<div>
<form>
<input id="demo1"/>
<input id="demo2">
</form>
</div>
</body>
<html>
To get the input 'demo2'
xpath="http://input[1]"
4 Arbitrary single attribute
<html>
<body>
<div>
<form>
<input id="demo1"/>
<input id="demo2" foo="bar">
</form>
</div>
</body>
<html>
To get input 'demo2'
xpath="http://input[@id='demo2']" (equivalent to By.id)
Or
xpath="http://input[@foo='bar']"
5 Arbitrary multiple attributes
<html>
<body>
<div>
<form>
<input id="1" type="submit" />
<input id="2" foo="bar"/>
<input id="3" type="submit" foo="bar"/>
</form>
</div>
</body>
<html>
To get 3rd input
xpath="http://input[@type='submit'][@foo='bar']"
Or
xpath="http://input[@type='submit' and @foo='bar']"
If use xpath="http://input[@type='submit' or @foo='bar']" here you'll get an array. You can get the List by driver.findElements(By.xpath(xpath)) (java). Otherwise you'll get the first element(If you just use driver.findElement). Because all of the 3 input elements meet your condition 'or' and it gives you the first one.
6 Contains attribute
<html>
<body>
<div>
<form>
<input id="1" type="submit" />
<input id="2" foo="bar" daddy="dog"/>
<input id="3" type="submit" foo="bar"/>
</form>
</div>
</body>
<html>
To get the second input
xpath="http://input[@daddy]"
Because only the second one has attribute 'daddy'
7 Inner searching
<html>
<body>
<div>
<form>
<input id="input1" daddy="dog" />
<input id="input2" daddy="pig"/>
</form>
</div>
<div>
<form>
<input id="input3" daddy="dog" />
<input id="input4" daddy="apple"/>
</form>
</div>
</body>
<html>
To get the second div
xpath="http://div[.//input[@daddy='dog'] and .//input[@daddy='apple']]"
Overall those are all I can work out for now. Hope it helps.
這篇關(guān)于如何通過除“類"之外的屬性直接找到 WebElements和“名稱"(例如“標題")的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!
相關(guān)文檔推薦
How can I detect integer overflow on 32 bits int?(如何檢測 32 位 int 上的整數(shù)溢出?)
Local variables before return statements, does it matter?(return 語句之前的局部變量,這有關(guān)系嗎?)
How to convert Integer to int?(如何將整數(shù)轉(zhuǎn)換為整數(shù)?)
How do I create an int array with randomly shuffled numbers in a given range(如何在給定范圍內(nèi)創(chuàng)建一個隨機打亂數(shù)字的 int 數(shù)組)
Inconsistent behavior on java#39;s ==(java的行為不一致==)
Why is Java able to store 0xff000000 as an int?(為什么 Java 能夠?qū)?0xff000000 存儲為 int?)
-
• 如何在 Java 日歷 API 中使用儒...
-
• 將 long 轉(zhuǎn)換為字節(jié)數(shù)組并將其...
-
• Eclipse 錯誤,“無法啟動選擇...
-
• Eclipse 的 ColdFusion 9 行調(diào)試器...
-
• 在 Java Swing 中啟用 Transfersu...
-
• 用 mockito 模擬單例...
-
• 如何使用 mockito 或 powermock 模...
-
• 圖片正在播放動畫,圖片可點...
-
• 為什么我們需要在 Java 中的...
-
• Java從數(shù)組中刪除重復項?...
-
• IntelliJ IDEA:“無法解析符號...
-
• 使用 Active Directory 的 Azure 上...
-
• 如何在 Java 日歷 API 中使用儒...
-
• 將 long 轉(zhuǎn)換為字節(jié)數(shù)組并將其...
-
• Eclipse 錯誤,“無法啟動選擇...
-
• Eclipse 的 ColdFusion 9 行調(diào)試器...
-
• 在 Java Swing 中啟用 Transfersu...
-
• 用 mockito 模擬單例...
-
• 如何使用 mockito 或 powermock 模...
-
• 圖片正在播放動畫,圖片可點...
-
• 為什么我們需要在 Java 中的...
-
• Java從數(shù)組中刪除重復項?...
-
• IntelliJ IDEA:“無法解析符號...
-
• 使用 Active Directory 的 Azure 上...
旅游公司
服裝服飾
機械設(shè)備
電子產(chǎn)品
政府協(xié)會
網(wǎng)絡(luò)營銷
環(huán)保科技
科技公司
家政服務
營銷型
環(huán)保
軟件開發(fā)
傳媒公司
金融服務
雙語
培訓機構(gòu)
零部件
教育培訓
博客主題
軸承
新聞資訊
視頻
進銷存系統(tǒng)
bootstrap
商城模板
商務合作
廣告設(shè)計
驗證碼
門戶
ar
OElove
漫畫網(wǎng)
全景
商城
區(qū)塊鏈
虛擬幣
你畫我猜
卡券
動畫特效
在線客服
地板
域名停放
canvas
html5
svg
博客
攝影
導航
小說源碼
代理公司
蘋果cms
微擎微贊
微商
訂單系統(tǒng)
小程序
電影源碼
微信程序
帝國cms
掃碼點餐
jquery
angular
視頻打賞
thinkphp
360
動畫模板
淘寶客
音樂
分發(fā)系統(tǒng)
o2o
微擎
主站蜘蛛池模板:
日韩在线免费播放
|
亚洲在线视频
|
波多野吉衣一二三区乱码
|
草草视频在线观看
|
亚洲一级免费视频
|
日韩一级av毛片
|
日本美女毛茸茸
|
午夜激情网
|
色综合天天综合网国产成人网
|
中文字幕免费在线观看
|
最新av在线
|
欧美国产精品一区二区
|
操操操日日日
|
91高清国产
|
韩国av免费
|
三上悠亚一区
|
天堂99|
黄色小说视频网站
|
黄色影视
|
久久久久九九九
|
天天插天天狠天天透
|
国产在线黄色
|
成人在线观看网站
|
成人国产精品免费观看
|
国产wwwwww
|
亚洲第一毛片
|
www亚洲精品
|
免费国产黄色
|
九九免费视频
|
亚洲成肉网|
国产区一区二区
|
一区二区三区在线看
|
亚洲高清在线视频
|
国产精品三|
欧美日韩精品一区
|
亚洲一区久久
|
中文在线视频
|
国产不卡一区
|
精品一区二区三区免费看
|
成人免费网站
|
高潮毛片无遮挡免费看
|