使用Java在Selenium Webdriver中的下拉值

时间:2017-05-17 12:15:03

标签: selenium

我处于这样一种情况,我必须扩展下拉列表,并在扩展后从值列表中,我必须选择一个值。

你能帮忙吗?

以下是我目前使用的代码:

List<WebElement> elements = driver.findElement(By.id("Some Value"));

for (WebElement element: elements)
{
    new Actions(driver).sendKeys(Keys.Arrow_Down).perform();
        if(Element.getText().equals("Cliam Document"))
        {
            element.click();
        }
}

3 个答案:

答案 0 :(得分:1)

您可以使用以下代码:

使用下拉列表

   Select select1 = new Select(driver.findElement(By.id("your identifier")));
    select1.selectByVisibleText("Cliam Document");

答案 1 :(得分:0)

您可以从3种方式下拉选择值

  1. 使用selectByVisibleText()
  2. 使用selectByValue()
  3. 使用selectByIndex()
  4. 在您的情况下,不需要使用Action()类。您可以使用上面的任何方法。

    检查此代码:

    Select se = new Select(driver.findElement(By.id("your ID")));
    se.selectByIndex(your preferred index value);
    

答案 2 :(得分:0)

<强>方案:
 1.将值存储到要执行单击操作的字符串中  2.将所有下拉值存储到列表中  3.使用For each循环将所有值存储到WebElement中  4.获取每个值的文本并存储到字符串中  5.将每个实际字符串与预期字符串进行比较  6.如果在每个循环中找到了预期的字符串,则单击WebElement。

String Expected_String = "Test";
List<WebElement> elements = driver.findElement(By.id("Some Value"));
for (WebElement element : elements) 
{
    String value = element.getText();
    if (value.equalsIgnoreCase( Expected_String)) 
    {
        element.click();
        break;
    }
}