使用selenium java按索引号选择列表值

时间:2012-07-11 23:14:12

标签: java drop-down-menu selenium webdriver

我尝试在代码中使用select()方法,但Eclipse显示错误。 select()是内置的Selenium方法吗?我不明白。

select(driver2.findElement(By.xpath("//*@id='webLossReport.contact.address.state']")),index=i);

Eclipse说"The method select(WebElement, int) is undefined for the type entry",它给了我一个在这个类中创建方法的选项。

请告诉我其他人如何使用它。我的要求是“根据索引号选择列表值”

更新:代码按要求发布,

WebElement LSD = driver2.findElement(By.xpath("//select[@id='webLossReport.lossInformation.locationOfLoss.state']"));
List <WebElement> LLS = LossStateDropdown.findElements(By.tagName("option"));

int i= LLS.size();      
select(driver2.findElement(By.xpath("//*@id='webLossReport.contact.address.state']")),index=i);

3 个答案:

答案 0 :(得分:3)

你在Selenium RC和Selenium WebDriver之间丢失了。假设您要使用WebDriver,see this doc, it explains it all

您可以执行以下操作 - 它会直接在指定的<option>中找到第三个<select>标记,然后点击它:

driver.findElement(By.xpath("id('selectsId')/option[3]")).click();

或使用Select class

Select sel = new Select(driver.findElement(By.id("selectsId")));
sel.selectByIndex(3);

答案 1 :(得分:1)

在C#中,我解决了这个问题:

selenium.Select("id=yourID", "index=" + i.ToString());

答案 2 :(得分:0)

我不熟悉此库,但the Selenium reference pageselect提供了以下签名:

select(java.lang.String selectLocator, java.lang.String optionLocator)

在您的代码中,第二个参数是index=i,它将index分配给i的值,然后返回int。你打算作为第二个参数传递什么字符串? "index=i""index=" + i