問題描述
我是 Java 和 Selenium 的新手,所以如果我的問題聽起來有點初級,我提前道歉.
我使用:
driverChrome.findElements(By.className("blabla"));
查找以blabla"作為其類名的元素,例如:
<span class="blabla" title="標題">...</span>
現在,如果我想通過其他屬性查找所有元素怎么辦?類似:
driverChrome.findElements(By.titleValue("the title"));
這是我目前用于執行此任務的代碼:
列表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']" 你會得到一個數組.您可以通過 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 內搜索
<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']]"
總的來說,這些都是我現在可以解決的.希望對您有所幫助.
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.
這篇關于如何通過除“類"之外的屬性直接找到 WebElements和“名稱"(例如“標題")的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!
相關文檔推薦
How can I detect integer overflow on 32 bits int?(如何檢測 32 位 int 上的整數溢出?)
Local variables before return statements, does it matter?(return 語句之前的局部變量,這有關系嗎?)
How to convert Integer to int?(如何將整數轉換為整數?)
How do I create an int array with randomly shuffled numbers in a given range(如何在給定范圍內創建一個隨機打亂數字的 int 數組)
Inconsistent behavior on java#39;s ==(java的行為不一致==)
Why is Java able to store 0xff000000 as an int?(為什么 Java 能夠將 0xff000000 存儲為 int?)
最新文章
-
• 如何在 Java 日歷 API 中使用儒...
-
• 將 long 轉換為字節數組并將其...
-
• Eclipse 錯誤,“無法啟動選擇...
-
• Eclipse 的 ColdFusion 9 行調試器...
-
• 在 Java Swing 中啟用 Transfersu...
-
• 用 mockito 模擬單例...
-
• 如何使用 mockito 或 powermock 模...
-
• 圖片正在播放動畫,圖片可點...
-
• 為什么我們需要在 Java 中的...
-
• Java從數組中刪除重復項?...
-
• IntelliJ IDEA:“無法解析符號...
-
• 使用 Active Directory 的 Azure 上...
熱門文章
-
• 如何在 Java 日歷 API 中使用儒...
-
• 將 long 轉換為字節數組并將其...
-
• Eclipse 錯誤,“無法啟動選擇...
-
• Eclipse 的 ColdFusion 9 行調試器...
-
• 在 Java Swing 中啟用 Transfersu...
-
• 用 mockito 模擬單例...
-
• 如何使用 mockito 或 powermock 模...
-
• 圖片正在播放動畫,圖片可點...
-
• 為什么我們需要在 Java 中的...
-
• Java從數組中刪除重復項?...
-
• IntelliJ IDEA:“無法解析符號...
-
• 使用 Active Directory 的 Azure 上...
熱門標簽
旅游公司
服裝服飾
機械設備
電子產品
政府協會
網絡營銷
環保科技
科技公司
家政服務
營銷型
環保
軟件開發
傳媒公司
金融服務
雙語
培訓機構
零部件
教育培訓
博客主題
軸承
新聞資訊
視頻
進銷存系統
bootstrap
商城模板
商務合作
廣告設計
驗證碼
門戶
ar
OElove
漫畫網
全景
商城
區塊鏈
虛擬幣
你畫我猜
卡券
動畫特效
在線客服
地板
域名停放
canvas
html5
svg
博客
攝影
導航
小說源碼
代理公司
蘋果cms
微擎微贊
微商
訂單系統
小程序
電影源碼
微信程序
帝國cms
掃碼點餐
jquery
angular
視頻打賞
thinkphp
360
動畫模板
淘寶客
音樂
分發系統
o2o
微擎
主站蜘蛛池模板:
亚洲美乳中文字幕
|
日本精品视频在线观看
|
无毛av
|
久久伊人一区二区
|
欧美成人免费在线视频
|
欧美日韩综合一区
|
激情一区二区三区
|
性一区
|
亚洲一区二区久久
|
日韩欧美久久精品
|
gogo肉体亚洲高清在线视
|
午夜影院在线观看
|
日韩精品免费一区二区在线观看
|
a级黄色片在线观看
|
久久精品国产免费看久久精品
|
国产一区二区电影网
|
热久久999
|
午夜影院黄|
久久国产精品一区二区三区
|
国产精品一区二
|
黄色一级片aaa
|
91偷拍精品一区二区三区
|
久久免费大片
|
av中文字幕在线观看
|
草草草久久久
|
亚洲天堂精品久久
|
亚洲激情综合
|
亚洲精品国产综合区久久久久久久
|
日韩毛片在线观看
|
色综合天天网
|
精品日韩在线观看
|
日韩在线
|
国产乱码一二三区精品
|
日韩欧美在线视频
|
一区二区三区在线看
|
日韩欧美一级精品久久
|
成人一区二区三区视频
|
成年人在线观看
|
国产精品不卡一区
|
国产伦精品一区二区三区高清
|
亚洲综合无码一区二区
|