通过祖先/父/等目标下拉框。?

时间:2017-08-17 13:47:57

标签: java selenium drop-down-menu parent ancestor

有问题的HTML代码

<div class="wrap-padding-sm">
  <div class="threeCols">
    <div class="colContent">
      <div class="sub-colContent">
        <span>
          <div id="cust_opt_TypeCodeCheckBox_" class="  ">
            <div>
              <div class="uniform-checkbox    checked">.
                <div class="checker col-xs-1 col-md-1" id="uniform-bunny:h_abc257:0:cust_opt_TypeCodeCheckBox_:select-boolean-checkbox" style="display: block;">
                  <span class="checked">
                    <input id="bunny:h_abc257:0:cust_opt_TypeCodeCheckBox_:select-boolean-checkbox" type="checkbox" name="bunny:h_abc257:0:cust_opt_TypeCodeCheckBox_:select-boolean-checkbox" checked="checked" class=" " onclick="mojarra.ab(this,event,'valueChange','bunny:custom_bunny_hops','bunny:custom_bunny_hops',{'onevent':recommendedBunnyAfterAJAX})">
                  </span>
                </div>
              </div>
            </div>
          </div>
        </span>
        <span class="checkTitle">This money</span>
      </div>
      <div class="sub-colContent">
        <div class="select-dropWrap">
          <div id="h_abc281" class="form-group form-group-lg">
            <label for="bunny:h_abc257:0:h_abc281:select-one-menu" class="control-label">up to </label>
            <div class="select-dropMenu  ">
              <div class="selector uniform-select fixedWidth" id="uniform-changeCoverages:j_idt257:0:j_idt281:select-one-menu">
                <span style="user-select: none;">600</span>
                <select id="bunny:h_abc257:0:h_abc281:select-one-menu" name="bunny:h_abc257:0:h_abc281:select-one-menu" class="form-control input-lg  " size="1" onchange="mojarra.ab(this,event,'valueChange','bunny:custom_bunny_hops','bunny:custom_bunny_hops',{'onevent':recommendedBunnyAfterAJAX})">
                  <option value="600" selected="selected">600</option>
                  <option value="800">800</option>
                  <option value="1000">1000</option>
                </select>
              </div>
            </div>
          </div>
        </div>
      </div>
      <span class="bunnyMsg h7 mobile-show"></span>
    </div>
    <div class="colContent">
      <a data-toggle="collapse" class="margin-R10" data-target="#cust_bunny_hops_0">
        <span>some text here</span>
      </a>
      <a data-toggle="collapse" data-target="#cust_bunny_hops_0" class="btn-accordian collapsed"></a>
    </div>
  </div>
  <div id="cust_bunny_hops_0" class="bunny-wrap collapse">
    <p>Some text here</p>
  </div>
  <span class="bunnyMsg h7 mobile-hide"></span>
</div>

我需要做的是目标

<span class="checkTitle">This money </span>

并从那里定位下拉菜单。 我设法使用

来定位它
Select dropDown = new Select(driver.findElement(By.xpath("//*[contains(text(), 'This money')]//ancestor::span[1]//select")));

并且在dropDown.selectByValue("800");时有效。如果我尝试选择另一个下拉菜单 StaleElementReferenceException ,现在会出现问题。

我通过更改此代码来解决StaleElementReferenceException

 Select dropDownTwo = new Select(driver.findElement(By.xpath("//*  [contains(text(), 'This money')]/following::select")));
 Select dropDownTwo = new Select(driver.findElement(By.xpath("//*  [contains(text(), 'Another drop down not in the HTML above')]/following::select")));
 dropDown.selectByValue("800");
 dropDownTwo.selectByValue("1000000");

到这个

 Select dropDownTwo = new Select(driver.findElement(By.xpath("//*  [contains(text(), 'This money')]/following::select")));
 dropDown.selectByValue("800");
 Select dropDownTwo = new Select(driver.findElement(By.xpath("//*  [contains(text(), 'Another drop down not in the HTML above')]/following::select")));
 dropDown.selectByValue("1000000");

1 个答案:

答案 0 :(得分:0)

根据提供的HTML,您似乎要查找的SELECT是您指定的参考标记之后的第一个SELECT

<span class="checkTitle">This money</span>

因此,您可以使用下面的XPath。

//span[contains(.,'This money')]/following::select