简体   繁体   English

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

[英]selecting list value by index number using selenium java

I tried using the select() method in my code, but Eclipse is showing an error. 我尝试在我的代码中使用select()方法,但Eclipse显示错误。 Is select() an inbuilt method of Selenium? select()是Selenium的内置方法吗? I don't get it. 我不明白。

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

Eclipse says "The method select(WebElement, int) is undefined for the type entry" and it is giving me an option to create a method in this class. Eclipse说"The method select(WebElement, int) is undefined for the type entry" ,它给了我一个在这个类中创建方法的选项。

Please let me know how others are using it. 请让我知道其他人如何使用它。 My requirement is to "select a list value based on Index number" 我的要求是“根据索引号选择一个列表值”

Update: Code Posted as requested, 更新:代码按要求发布,

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);

You're somehow lost between Selenium RC and Selenium WebDriver. 你在Selenium RC和Selenium WebDriver之间迷路了。 Assuming you want to use WebDriver, see this doc, it explains it all . 假设您要使用WebDriver, 请参阅此文档,它解释了所有内容

You can either do the following - it directly finds the third <option> tag in the specified <select> and clicks it: 您可以执行以下操作 - 它直接在指定的<select>找到第三个<option>标记并单击它:

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

or this using the Select class : 或使用Select

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

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

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

I'm not familiar with this library, but the Selenium reference page gives the following signature for select : 我不熟悉这个库,但Selenium参考页面select提供了以下签名:

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

In your code, the second argument is index=i which is assigning index to the value of i , and then returning that int . 在代码中,第二个参数是index=i被分配index到的值i ,然后返回该int What string were you planning on passing as the second argument? 你打算作为第二个参数传递什么字符串? "index=i" ? "index=i" "index=" + i ? "index=" + i

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM