Tuesday, 29 October 2013

How to select the value from the web list in selenium web driver in java?

Example -  We can select the value from the drop down by using 3 methods.
  1. selectByVisibleText - select by the text displayed in drop down
  2. selectByIndex  - select by index of option in drop down
  3. selectByValue  - select by value of option in drop down

Consider below code. Suppose you want to select the first option in the combo box.

<select id="44">
 <option value="1">xyz</option>
 <option value="2">abc</option>
 <option value="3">pqr</option>
</select>

Complete Example in Selenium Webdriver in Java

Below Java example shows how you can select the first value from the combo box using selenium webdriver.

//import the required packages and classes.

import java.io.File;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;




@SuppressWarnings("unused")
public class OpenGoogle {
    
public static void main(String [] arg) 
{

//set the path of the chrome driver exe file

System.setProperty("webdriver.chrome.driver", "C:\\Selenuim\\chromedriver2.8.exe");

//create the new instance of the chrome browser
WebDriver driver =  new ChromeDriver();


try{

//set the implicit and page load time outs  

driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(50,TimeUnit.SECONDS);

//navigate to given url
driver.get("https://www.google.co.in/preferences");

//Maximize the browser window
driver.manage().window().maximize();

WebElement e = driver.findElement(By.id("44"));
Select selectElement=new Select(e);

// both of the below statements will select first option in the weblist
selectElement.selectByVisibleText("xyz");  

selectElement.selectByValue("1");

Thread.sleep(2000);

}

catch(Exception e){
 System.out.println("Exception - > " + e.toString());
 }
 finally{
  driver.close();
  driver.quit();
 }
} //main function ends
 



}//class ends


You may also like to read below topics.
  1. Press key in Selenium Webdriver in Java
  2. Read Table Data in Selenium Webdriver in Java
  3. Take the screenshot in Selenium Webdriver in Java
  4. Execute JavaScript in Selenium Webdriver in Java
  5. Handle multiple browser windows in Selenium Webdriver in Java
  6. Double-Click on the Element in Selenium Webdriver in Java
  7. Exceptions in Selenium Webdriver in Java
  8. Synchronization in Selenium Webdriver in Java

What do you think on above selenium topic. Please provide your inputs and comments. Thanks

No comments:

Post a Comment

Buy Best Selenium Books