How will you select a particular value in a dropdown without using the methods of Select class in Selenium?

Software TestingAutomation TestingSelenium Web Driver

We can select a particular value in a dropdown using the method of Select class by using findElements() method.

Example

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import java.util.List;
import org.openqa.selenium.support.ui.Select;
public class OptionsClick{
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url = "https://www.tutorialspoint.com/tutor_connect/index.php";
      driver.get(url);
      driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
      // getting the list of elements with the xpath
      List<WebElement> opt = driver.findElements(By.xpath("//select[@name=’selType’]//option"));
      int s = opt.size();
      // Iterating through the list selecting the desired option
      for( int j = 0; j< opt.size();j++){
         // if the option is By Subject click that option
         if( opt.get(j).getText().equals("By Subject")){
            opt.get(j).click();
            break;
         }
      }
      driver.quit();
   }
}
raja
Published on 11-Jun-2020 15:22:02
Advertisements