如何使用selenium webdriver选择下拉值

时间:2017-04-05 12:58:55

标签: javascript html css

我的代码:

WebElement objSearchBy = wait.until(
    ExpectedConditions.presenceOfElementLocated(
        By.xpath("//html/body/div[I]/div[2]/form/div[2]/label[I]")
    )
);

HTML代码:

HTML code for drop-down title name

获得以下输出:

  

线程“main”中的异常org.openqa.selenium.TimeoutException:预期条件失败:等待位于以下位置的元素的存在:By.xpath:// html / body / div [I] / div [2] / form / div [2] / label [I](尝试30秒,间隔500 MILLISECONDS)

2 个答案:

答案 0 :(得分:0)

展开SELECT标记并查看其下的可用选项。基于此代码的第二行所需的选项更新。

Select dropdown = new Select(driver.findElement(By.id("loc_code")));
dropdown.selectByVisibleText("your option text");

Select dropdown = new Select(driver.findElement(By.id("loc_code")));
dropdown.selectByValue("option2");

dropdown.selectByIndex(2);

答案 1 :(得分:0)

请检查xpath正确性:如果是自动生成的,则值div[I]label[I]是否正确?

尝试在此处#loc_code使用有意义的路径或精确标识符