Articles → SELENIUM → Selectbyvalue, Selectbyindex And Selectbyvisibletext Methods In Selenium

Selectbyvalue, Selectbyindex And Selectbyvisibletext Methods In Selenium






HTML Code




<!DOCTYPE html>
<html lang="en"
	xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta charset="utf-8" />
		<title></title>
	</head>
	<body>
		<select id="AllOptions">
			<option value="1">Option 1</option>
			<option value="2">Option 2</option>
			<option value="3">Option 3</option>
			<option value="4">Option 4</option>
		</select>
	</body>
</html>




Picture showing the html page with dropdown
Click to Enlarge


Selectbyvalue




package com.FirstSeleniumProject;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

public class MyClass {
	@Test
	public void TestAnnotation() {
		WebDriver driver;
		String service = "IEDriverServer path";
		System.setProperty("webdriver.ie.driver", service);
		driver = new InternetExplorerDriver();
		driver.get("http://localhost:18275/Page1.html");

		Select selectObject = new Select(driver.findElement(By.id("AllOptions")));
		selectObject.selectByValue("3");
	}
}




Picture showing selecting the dropdown using selectByValue method in selenium
Click to Enlarge


Selectbyindex




package com.FirstSeleniumProject;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

public class MyClass {
  @Test
	public void TestAnnotation() {
		WebDriver driver;
		String service = "IEDriverServer path";
		System.setProperty("webdriver.ie.driver", service);
		driver = new InternetExplorerDriver();
		driver.get("http://localhost:18275/Page1.html");

		Select selectObject = new Select(driver.findElement(By.id("AllOptions")));
		selectObject.selectByIndex(1);
	}
}




Picture showing selecting the dropdown using selectByIndex method in selenium
Click to Enlarge


Selectbyvisibletext




package com.FirstSeleniumProject;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

public class MyClass {
  @Test
	public void TestAnnotation() {
		WebDriver driver;
		String service = "IEDriverServer path";
		System.setProperty("webdriver.ie.driver", service);
		driver = new InternetExplorerDriver();
		driver.get("http://localhost:18275/Page1.html");

		Select selectObject = new Select(driver.findElement(By.id("AllOptions")));
		selectObject.selectByVisibleText("Option 4");
	}
}




Picture showing selecting the dropdown using selectByVisibleText method in selenium
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Saturday, April 29, 2017

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250