需要在Selenium Webdriver中使用xpath选择下拉值

时间:2013-09-25 08:46:01

标签: selenium

我的目的是从RedBus站点的下拉列表(From字段)中选择一个值。我正在使用Xpath来选择它。

我使用以下代码:

WebDriver driver=new FirefoxDriver();
        driver.get("http://www.redbus.in/");
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
        driver.findElement(By.id("DDLSource")).sendKeys("Chenn");
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
        driver.findElement(By.xpath(".//*[@id='123']")).click();

但它不起作用。仅发送值但不选择。

任何人都可以帮帮我....

3 个答案:

答案 0 :(得分:2)

尝试

Select selectBox = new Select(driver.findElement(By.id("DDLSource")));
selectBox.selectByVisibleText(aText);

答案 1 :(得分:2)

要点击下拉列表中的某些内容,您必须使用WebDriver功能:

new Select(dropdownElement).selectByVisibleText(textValue);

dropdownElement是一个WebElement,你可以在那里使用driver.findElementBy ......

答案 2 :(得分:1)

我在firefox中测试了以下代码,但它确实有效。如果你想选择像chennai这样的城市,那么只需在from文本框中输入c,你就会得到一个以c开头的所有城市的列表。这也将使下拉元素变得可见。在此之后使用xpath并更改城市名称以在下拉列表中选择它。希望这可以帮助。快乐的编码。

WebDriver driver = new FirefoxDriver();
        driver.get("http://www.redbus.in");
        driver.findElement(By.xpath("//input[@id = 'DDLSource']")).sendKeys("c");
 //Pass the city name like Chennai instead of Chakshu
            driver.findElement(By.xpath("//dl[@id = 'lis']//dt[text()='Chakshu']")).click(); 
相关问题