【问题标题】:org.openqa.selenium.support.ui.UnexpectedTagNameException: Element should have been "select" but was "input"org.openqa.selenium.support.ui.UnexpectedTagNameException:元素应该是“选择”但是“输入”
【发布时间】:2016-03-18 07:54:57
【问题描述】:

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

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>

【问题讨论】:

  • 没有类似的数据:- 您尝试使用代码搜索的 HTML 中的 Arria
  • 没有id为“reporting_profile_id”的选择元素

标签: selenium webdriver


【解决方案1】:

Selenium.Support.UTI 中的 Select 类仅支持原生 select 元素,即

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

但是您的应用程序正在使用 Select2,它不是选择元素。在这种情况下,您必须单击&lt;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();

【讨论】:

  • 谢谢布班。如果没有
  • 正如我在回答中提到的,它可能是&lt;li&gt;&lt;a&gt;。您能给我看完整的 HTML(屏幕截图或文件)吗?
  • 我已经更新了我的答案。您可以尝试示例代码吗?如果不起作用,请尝试将XPath表达式更改为By.xpath("//li[contains(text()='Arria')]")By.xpath("//span[contains(text()='Arria')]")By.xpath("//a[contains(text()='Arria')]")
猜你喜欢
相关资源
最近更新 更多
热门标签