【问题标题】:Selenium is not selecting the correct valueSelenium 没有选择正确的值
【发布时间】:2016-09-01 11:41:22
【问题描述】:

我正在使用 selenium 测试具有选择选项和一种输入类型文本的网页。

手动并使用 selenium IDE,它可以正常工作,但是当我将测试用例导出到 Java Junit 时,我可以看到下拉单击,但 selenium 没有选择值,它只是在扩展下拉列表。

我能做什么?

让我们检查一下我的代码:

Select dropdown = new Select(driver.findElement(By.id("type")));
dropdown.selectByVisibleText("Celcius");

考虑一下我的表格,例如:

<form action="doit">
    <select name="type" id = "type">
        <option value = "fail"> Fail </option>
        <option value = "celcius"> Celcius </option>        
    </select>
    <input type="number" name="num" id="num">
</form>

【问题讨论】:

  • 可能是因为文字前后的空格?为什么不考虑使用selectByIndexselectByValue 来提高精度?

标签: java selenium


【解决方案1】:

有时这种方法行不通。如果选项标签之间的文本在标签前后有空格,则可能会发生这种情况。 例如。

1.&lt;option&gt; Fail &lt;/option&gt; 2.&lt;option&gt;Fail&lt;/option&gt;

在上面的例子中1和2是不同的。 所以你可以使用like,

driver.findElement(By.id("type")).click();     // this will expand the list
driver.findElement(By.xpath("//select[@id='type']/option[contains(text(),'Celcius')]")).click();

也尝试直接点击。如果元素是可存活的,它就可以工作,

driver.findElement(By.xpath("//select[@id='type']/option[contains(text(),'Celcius')]")).click();

【讨论】:

  • 无 :( 我认为这是我的 Selenium java 代码的一些问题......使用接口它工作得很好,但在 java 单元的情况下,它没有。
【解决方案2】:

driver.findElement(By.id("type")).sendkeys("摄氏度");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    相关资源
    最近更新 更多