如何使用selenium webdriver单击列表中的特定元素?
问题描述:
- 我的名字来自excel表
-
元素列表,具有此特定名称,我需要点击此元素
这是我的代码:
- I have a name which is coming from excel sheet
List of elements, having this particular name and i need to click on this element Here is my code:
for(WebElement li : myElements)
{
System.out.println(li.getText());
System.out.println();
for (int i=0; i<Name.length(); i++)
{
if(li.getText().equalsIgnoreCase(Name[i]))
{
System.out.println("Matched");
//myElements.get(2).click();
}
}
}
这是我的html代码:
Here is my html code:
<div style="margin-right:30px;" class="rlbGroup rlbGroupRight">
<ul class="rlbList">
<li class="rlbItem rlbHovered" id="radListBoxSource_i0">
<span class="rlbText">Apex</span></li><li class="rlbItem" id="radListBoxSource_i1">
<span class="rlbText">ARC</span></li><li class="rlbItem" id="radListBoxSource_i2">
<span class="rlbText">Buckeye</span></li><li class="rlbItem" id="radListBoxSource_i3">
<span class="rlbText">Citgo</span>
</li>
<li class="rlbItem" id="radListBoxSource_i4">
<span class="rlbText">Colonial</span>
</li>
<li class="rlbItem" id="radListBoxSource_i5">
<span class="rlbText">KMEP North</span>
</li>
<li class="rlbItem" id="radListBoxSource_i6">
<span class="rlbText">KMEP South</span>
</li>
<li class="rlbItem" id="radListBoxSource_i7">
<span class="rlbText">KMEP West</span>
</li>
<li class="rlbItem" id="radListBoxSource_i8">
<span class="rlbText">Magellan</span>
</li>
<li class="rlbItem" id="radListBoxSource_i9">
<span class="rlbText">Magellan East</span>
</li>
<li class="rlbItem" id="radListBoxSource_i10">
<span class="rlbText">Magellan West</span>
</li>
<li class="rlbItem" id="radListBoxSource_i11">
<span class="rlbText">Marathon</span>
</li>
<li class="rlbItem" id="radListBoxSource_i12">
<span class="rlbText">Marathon East</span>
</li>
<li class="rlbItem" id="radListBoxSource_i13">
<span class="rlbText">Motiva</span>
</li>
<li class="rlbItem" id="radListBoxSource_i14">
<span class="rlbText">Motiva Shell</span>
</li>
<li class="rlbItem" id="radListBoxSource_i15">
<span class="rlbText">Motiva South</span>
</li>
<li class="rlbItem" id="radListBoxSource_i16">
<span class="rlbText">TPSI</span>
</li>
<li class="rlbItem" id="radListBoxSource_i17">
<span class="rlbText">TPSI East</span>
</li>
<li class="rlbItem" id="radListBoxSource_i18">
<span class="rlbText">Vecenergy</span>
</li>
</ul>
</div>
此名称以excel表格中的'Motiva'作为字符串。
In this name is coming as'Motiva' from excel sheet as string.
任何人都可以帮我解决这个问题吗?
Can anyone help me on this issue?
答
这里有:
假设你的myElements如下:
Here it goes: Assuming your myElements will be as below:
List<WebElement> myElements =driver.findElements(By.cssSelector("li[class*='rlbItem']"));
for(WebElement li : myElements)
{
System.out.println(li.getText());
System.out.println();
for (int i=0; i<Name.length(); i++)
{
if(li.getText().equalsIgnoreCase(Name[i]))
{
//Clicks on the matched webelement
li.click();
}
}
}