使用 selenium java webdriver 从下拉列表中选择一个值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17922390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
select a value from drop down in selenium java webdriver using
提问by Jyotsna Bysana
how to select a value from dropdown using selenium java webdriver using xpath? depending on the option selected in the dropdown, fields appear .so that I need t enter values in it. My problem is I am not getting the fields after selecting the option in dropdown.After a long period of time it is appears, mean while error appears
如何使用 xpath 使用 selenium java webdriver 从下拉列表中选择一个值?根据下拉列表中选择的选项,字段会出现。因此我不需要在其中输入值。我的问题是在下拉列表中选择选项后我没有得到字段。经过很长一段时间后,它出现,意味着出现错误
回答by pradeep_tester
You can click the drop down and wait for the options to get displayed and then you can click the option from it.
您可以单击下拉菜单并等待显示选项,然后您可以单击其中的选项。
or
或者
Select select = new Select(driver.findElement(By.id("drop_down_id")));
select.selectByIndex(`index_value_of_option`);
回答by nilamber
Jyotsna... Your script need to wait till the field appear. For this you need to use any of the wait condition.
Jyotsna... 您的脚本需要等到该字段出现。为此,您需要使用任何等待条件。
implicit wait
隐式等待
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
or sleep condition
或睡眠状况
Thread.sleep(2000);
or you can use Fluent wait (the best one as per my suggestion)
或者你可以使用 Fluent 等待(根据我的建议最好的一个)
public WebElement fluentWait(final By locator) {
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
return driver.findElement(locator);
}
});
return foo;
};
fluentWait function returns your found web element. From the documentation on fluentWait: An implementation of the Wait interface that may have its timeout and polling interval configured on the fly. Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page. Details you can get here
fluentWait 函数返回您找到的 Web 元素。来自 fluentWait 的文档:Wait 接口的一个实现,它可以动态配置超时和轮询间隔。每个 FluentWait 实例定义等待条件的最长时间,以及检查条件的频率。此外,用户可以配置等待以在等待时忽略特定类型的异常,例如在页面上搜索元素时的 NoSuchElementExceptions。您可以在这里获得详细信息
Usage of `fluentWait in your case be the following:
在您的情况下使用 `fluentWait 如下:
WebElement textbox = fluentWait(By.id("textbox"));
回答by Krushna Chulet
Sample statements to open browser, load URL and select value from dropdown
打开浏览器、加载 URL 和从下拉列表中选择值的示例语句
static WebDriver driver;
System.setProperty("webdriver.ie.driver","C:\(Path)\IEDriverServer.exe");
driver = new InternetExplorerDriver();
driver.manage().window().maximize();
driver.get("EnterURLHere");
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
Select value1 = new Select(driver.findElement(By.id("LocateId")));
value1.selectByVisibleText("ValueToBeSelected"); //Select Character from dropdown list
回答by Tapan Khimani
you can add wait so that the problem of late loading would be resolve.
您可以添加等待,以便解决延迟加载的问题。
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
or,
或者,
Thread.sleep(2000);
For selecting from drop down , there are multiple ways from which one can select :
对于从下拉菜单中选择,有多种方式可供选择:
Select dropdown = new Select(driver.findElement(By.id(""))); // By id
dropdown.selectByVisibleText(""); // By Visible text
dropdown.selectByIndex(1); // By index