简体   繁体   中英

How to click on particular element in a list using selenium webdriver?

  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(); } } } 

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>

In this name is coming as'Motiva' from excel sheet as string.

Can anyone help me on this issue?

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();
        }
      }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM