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
-
do you have any code?Pedro del Sol– Pedro del Sol2013-07-29 11:32:58 +00:00Commented Jul 29, 2013 at 11:32
-
You need to use wait for element strategy until the field appearsHarshavardhan Konakanchi– Harshavardhan Konakanchi2013-07-30 10:00:07 +00:00Commented Jul 30, 2013 at 10:00
4 Answers
Jyotsna... Your script need to wait till the field appear. For this you need to use any of the wait condition.
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)
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
Usage of `fluentWait in your case be the following:
WebElement textbox = fluentWait(By.id("textbox"));
Comments
Sample statements to open browser, load URL and select value from dropdown
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
Comments
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