一尘不染

如何使用Selenium WebDriver单击列表中的特定元素?

selenium

  1. 我有一个名字来自Excel工作表
  2. 具有此特定名称的元素列表,我需要单击此元素,这是我的代码:
        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代码:

       <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’的形式出现。

谁能帮我解决这个问题?


阅读 398

收藏
2020-06-26

共1个答案

一尘不染

事情就这样了:假设您的myElements将如下所示:

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