【问题标题】:Locating select element in Selenium在 Selenium 中定位选择元素
【发布时间】:2012-11-03 16:56:48
【问题描述】:

下面是组合框:

<TD ALIGN="left" id="oldcontent">
<select name="status" style="width=150" id="newcontentformat"><option value="14" selected="selected">text1</option>
<option value="15">text2</option>
<option value="16">text3</option></select>
</TD>

我需要在组合框/下拉菜单中选择 text2。我使用了以下内容:

selenium.select("//select[starts-with(@name,status)","text2");

我面临的问题是,它给了我一个错误 text2 not found。由于在此同名状态上方可能还有其他选择框。所以我需要选择 2 个下拉/组合框的第二个元素。

请给我解决方案。它很紧急。在此先感谢

另一个问题

<TD align="left" WIDTH="18%"><FONT ID="oldContent">
<select name="status" onchange="selectTime(this.options[this.selectedIndex].value)" id="newcontentformat">
<option value="" selected="selected"></option>
<option value="1">text100</option>
<option value="2">text200<</option>
<option value="3">text300<</option>
</TD>

【问题讨论】:

  • 你能再发一个 TD 标签,我可以给你发所需的答案吗?
  • 我希望下面的答案能正常工作,因为 TD 标签的 id 不同
  • 它在 iFrame 中吗?也可以试试//select[starts-with(@name,'status')

标签: selenium selenium-rc selenium-webdriver selenium-ide


【解决方案1】:

我希望这应该工作

selenium.select("//td[@id='oldcontent']/select","label=text2")

【讨论】:

  • 我在上面又发布了一个 TD TAG
【解决方案2】:

您是否尝试过选择标签的 id。

selenium.select("//select[@id='newcontentformat']","label=text2");

                               or

selenium.select("//td[@id='oldContent']/select[@id='newcontentformat']","label=text2");

【讨论】:

    【解决方案3】:

    好吧。我会尝试使用 css 选择器解决方案:

    String cssDropdown="select[id='newcontentformat']";
    String csstext200="select[id='newcontentformat']>option[value='2']";
    driver.findElement(By.cssSelector(cssDropdown)).click();
    driver.manage().timeouts().implicitlyWait(1,TimeUnit.SECONDS);
    //Thread.sleep(1000);
    driver.findElement(By.cssSelector(csstext200)).click();
    

    第二种方法: 如果支持 jQuery,您可以使用以下内容:

     JavascriptExecutor js = (JavascriptExecutor) driver;
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("var x=$(\'"+csstext200+"\');");
            stringBuilder.append("return x.click()");
            (String) js.executeScript(stringBuilder.toString());
    

    希望对你有帮助

    【讨论】:

      【解决方案4】:

      虽然这个解决方案不是绝对的,但它会起作用。 您可以使用以下命令找到此下拉列表: //select[@name='Status']/following::select[@name='Status'] 您可以根据下拉列表的数量添加更多关注。你也可以在它之前使用你想倒着开始。 请通过 Selenium.1.0.Testing.Tools.Beginners.Guide Ebbok。

      【讨论】:

        【解决方案5】:

        WebDriver driver = new InternetExplorerDriver(); driver.get("http://agra.quikr.com/"); driver.findElement(By.xpath("//select[@id='alertcatSelectBox']")).sendKeys("Jobs");

        // 为您的示例 driver.findElement(By.xpath("//select[@name='status']")).sendKeys("text2");

        【讨论】:

          猜你喜欢
          • 2012-05-23
          • 2015-03-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-24
          相关资源
          最近更新 更多