从下拉菜单中选择元素

火箭

我想在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测试网站。我如何从下拉菜单中选择“节多普达”选项?

哈里·基申

我已经在您提到的上述网站上尝试过,并且对我有用。实际上,您需要使用自定义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();
                   }
                  }         

        }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何根据用户从下拉菜单中选择的参数对Java对象列表进行排序?

如何从下拉菜单中选择一个选项

RSelenium:从下拉菜单中选择选项

Jsoup从下拉菜单中选择元素

如何从下拉菜单中选择文本和值?

使用innerHTML向页面添加新选择会导致以前添加的选择元素丢失用户从下拉菜单中选择的任何值

如何通过Selenium和python从下拉菜单中选择元素?

从下拉菜单中选择元素后,如何显示它?

输入输入数字并从下拉菜单中选择时自动获取结果

无法从下拉菜单中选择值

如何使用Selenium和Python从下拉菜单中选择一个选项

在Selenium Python的下拉菜单中选择动态元素

模拟javascript以从下拉菜单中选择

从下拉菜单中选择选项后的事件

从下拉菜单中选择值

从下拉菜单中选择项目

python硒从下拉菜单中选择

从下拉菜单中选择某些选项时,选中相应的复选框?

允许用户从下拉菜单中选择选项

检查用户是否从下拉菜单中选择一个选项

从下拉菜单中选择普通JavaScript删除项目

当下拉菜单具有相同的ID时,如何从下拉菜单中选择一个选项

根据从下拉菜单中选择的内容自动填充输入

如何从下拉菜单中选择mysqli表ID号并从该行输出选择值

量角器在下拉菜单中选择元素

Python - 从下拉菜单中选择选项

保存从下拉菜单中选择的反应表

从下拉菜单中选择时出现RSelenium问题

Python Selenium - 从下拉菜单中选择值