org.openqa.selenium.support.ui.UnexpectedTagNameException:元素应该是" select"但是"输入"

时间:2016-03-15 00:49:50

标签: selenium webdriver

以下代码用于一周前工作,但现在我收到错误:

  

org.openqa.selenium.support.ui.UnexpectedTagNameException:元素应该是"选择"但是"输入"

请帮忙。

  public analyticsLandingPage verifyReportingProfile() throws InterruptedException{
    Select select = new Select(driver.findElement(By.xpath(".//* [@id='reporting_profile_id']")));
    select.selectByVisibleText("Arria");
    Thread.sleep(1000L);
    //reporting_profile.findElement(By.xpath(//[@id='s2id_reporting_profile_id']/a")).isDisplayed();
    //reporting_profile.sendKeys("Arria");
    //reporting_profile.sendKeys(Keys.TAB);
    reporting_profile.isDisplayed();

页面HTML:

    <div class="form-group">
     <label class="col-sm-2 control-label"     for="reporting_profile_id">Reporting profile</label>
    <div class="col-sm-10">
    <div id="s2id_reporting_profile_id" class="select2-container select2-allowclear reporting_profile" style="width: 50%;">
    <a class="select2-choice" tabindex="-1" href="javascript:void(0)">
    <span id="select2-chosen-7" class="select2-chosen">Agency B - No Adwords Cost or Conversions</span>
    <abbr class="select2-search-choice-close"></abbr>
    <span class="select2-arrow" role="presentation">
   </a>
   <label class="select2-offscreen" for="s2id_autogen7">Reporting profile</label>
   <input id="s2id_autogen7" class="select2-focusser select2-offscreen" type="text" role="button" aria-haspopup="true" aria-labelledby="select2-chosen-7">
    </div>
     <input id="reporting_profile_id" class="reporting_profile" 
      type="hidden" name="reporting_profile_id" tabindex="-1 
      title="Reporting    profile" style="display: none;" value="12">
    </div>
    </div>

1 个答案:

答案 0 :(得分:0)

Selenium.Support.UTI中的Select类仅支持本机选择元素

<select>
  <option>Item 1</option1>
  <option>Item 2</option1>
  ...

但您的应用程序使用的是Select2,而不是select元素。在这种情况下,您必须点击<a class="select2-choice",然后您必须找到选项元素(可能是lia的列表)。请参阅下面的示例代码:

driver.findElement(By.cssSelector("#s2id_reporting_profile_id > a.select2-choice")).click();
driver.findElement(By.xpath("//*[contains(text()='Arria')]")).click();