【问题标题】:Select element from a dropdown [duplicate]从下拉列表中选择元素[重复]
【发布时间】:2015-09-30 07:21:35
【问题描述】:

我想在 Joomla 中测试一个应用程序。 我有一个包含此代码的下拉列表:

<div class="control-group">
    <div class="control-label">
        <label id="jform_category-lbl" for="jform_category" class="required">
            Categoria<span class="star">&#160;*</span>
        </label>
    </div>
    <div class="controls">
        <select id="jform_category" name="jform[category]" class="required" required aria-required="true">
            <option value="9">stanza singola</option>
            <option value="10">stanza doppia</option>
            <option value="11">posto letto</option>
        </select>
    </div>
</div>

我正在使用 Java 来测试网站。 如何从下拉列表中选择“stanza doppia”选项?

【问题讨论】:

  • 创建Select 对象会有所帮助,这在第一个答案中提到。

标签: java selenium joomla


【解决方案1】:

我已经在您提到的上述网站上进行了尝试,它对我有用。 实际上,您需要使用 custom xpath 从下拉列表中选择值并将其存储在列表中。然后点击你想要的值。

有时 Select() 不起作用,您可以使用此解决方法来选择下拉列表中的值。

这是相同的工作代码。

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public class SelectDropdown {

    public static void main(String[] args) {

        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        driver.get("http://bachecalloggi.listedisinistra.org/index.php/annunci");
        driver.findElement(By.xpath("//button[contains(text(),'Ricerca Avanzata')]")).click();
        select1(driver);
    }

        public static void select1(WebDriver driver) {
                  //Clicking on the Element to open the dropdown. 
                  WebElement clickme = driver.findElement(By.xpath("//*[@id='filter_energy_class_chzn']/a/span"));
                  clickme.click();
                  //Sometime need to wait, as it take some time to load the values in dropdown.
                  //Thread.sleep(3000);

                  //Picking all the value from Dropdown. Use Custom Xpath for this.
                  List<WebElement> options = driver.findElements(By.xpath("//*[@id='filter_energy_class_chzn']//*[@class='chzn-results']/li"));

                  for (int i=0; i<options.size(); i++){
                   if (options.get(i).getText().equalsIgnoreCase("B")){
                    options.get(i).click();
                   }
                  }         

        }
}

【讨论】:

    【解决方案2】:

    您是否考虑过使用Select

    WebElement elemnet = driver.findElement(By.id("jform_category"));
    Select select = new Select(elemnet);
    //By value
    select.selectByValue("10");
    
    //By index
    select.selectByIndex(2);
    
    //By text
    select.selectByVisibleText("stanza doppia");
    

    【讨论】:

    • 我尝试过,但它不起作用:(我在公共页面上遇到同样的问题:bachecalloggi.listedisinistra.org/index.php/annunci 这是代码:`public void testFilterAds() throws Exception { driver.findElement(By .xpath("//button[contains(text(),'Ricerca Avanzata')]")).click(); WebElement elemnet = driver.findElement(By.id("filter_energy_class")); Select select = new Select (elemnet); //按值 select.selectByValue("b"); //按索引 select.selectByIndex(2); //按文本 select.selectByVisibleText("B"); Thread.sleep(500000); }`
    • 未选择任何内容 :(
    • 你使用的代码和我不一样
    • 你能在bachecalloggi.listedisinistra.org/index.php/annunci页面的filter_energy_class下拉列表中发布一个完整的类来选择B吗?请
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多