如何自动化下拉列表,打开一个文本框,使用Selenium从下拉列表中选择特定值

时间:2017-01-05 07:06:00

标签: selenium automation

我有以下代码 -

     WebElement dropdown = driver.findElement(By.xpath("//select[@id='DRPDWNcasTyp']")); 
     Select select = new Select(dropdown); 
     java.util.List<WebElement> options = select.getOptions(); 
     for(WebElement item:options) 
     { 

         System.out.println("Dropdown values are "+ item.getText());
         if(item.getText().equals("Others"))
         {                                     
              WebElement otherct = driver.findElement(By.xpath("//select[@id='otherCaseType']")); 
              otherct.click();             
              otherct.sendKeys("Delhi Judiciary board"); 
         }
     }

这不起作用。选择值"Others"后,文本框应显示为&amp;它应该接受输入。 if条件中的代码无效。

Html代码如下:

<select data-val="true" data-val-number="The field Case Type must be a number." data-val-required="This field can not be empty." id="DRPDWNcasTyp" name="caseTypeID" class="valid">
    <option value="">--Select--</option>

1 个答案:

答案 0 :(得分:0)

如果选择“其他”选项会显示文本框,那么您可以尝试使用代码段:

        WebDriverWait wait = new WebDriverWait(driver, 120);
        WebElement dropdown = driver.findElement(By.xpath("//select[@id='DRPDWNcasTyp']")); 
        Select select = new Select(dropdown); 

        select.selectByVisibleText("Others");

        WebElement otherct = driver.findElement(By.xpath("//select[@id='otherCaseType']"));
        wait.until(ExpectedConditions.visibilityOf(otherct)); //wait until desired textbox appears upto 120 seconds
        otherct.sendKeys("Your desired text");