如何使用selenium webdriver单击列表中的特定元素? [英] How to click on particular element in a list using selenium webdriver?

查看:110
本文介绍了如何使用selenium webdriver单击列表中的特定元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  1. 我的名字来自excel表

  2. 元素列表,具有此特定名称,我需要点击此元素
    这是我的代码:

  1. I have a name which is coming from excel sheet
  2. 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();
        }
      }
}

这篇关于如何使用selenium webdriver单击列表中的特定元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆