I am new with automation testing and right now I'm trying to select values in drop down menu. As I understand there are 2 drop downs in my example, but lack of experience makes it difficult to get the point how to solve this problem. I am working right now on https://www.spicejet.com/ and what I want to do is select passengers after that click on adults and set how many adults should be.
I've been watching few videos how to select dropdowns, few people suggest to use simple driver and use clicks other to create Select object and use it. Have not much code written, because of errors. Also, feeling lost with 'Select' as I understand I create new object 's', passing driver object to him and doing things?
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class dropdown {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.spicejet.com/"); // URL in the browser
driver.manage().window().maximize(); // Maximize the browser
Select s = new Select(driver.findElement(By.id("ctl00_mainContent_ddl_originStation1")));
s.selectByValue("2");
}
}
This one works ->
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class dropdown {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.spicejet.com/"); // URL in the browser
driver.manage().window().maximize(); // Maximize the browser
// Get specific area to save it as variable and check it later if we are in right web page
String verifyPage = driver.findElement(By.xpath("//span[contains(text(),'Flights')]")).getText();
// Check it with IF
if (verifyPage.contentEquals("Flights")) {
System.out.println("[1] You are IN the right page.");
} else {
System.out.println("[2] You are NOT in the right page.");
}
driver.findElement(By.xpath("//div[@id='divpaxinfo']")).click();
Select dropdown = new Select(driver.findElement(By.xpath("//select[@id='ctl00_mainContent_ddl_Adult']")));
dropdown.selectByIndex(1);
}
}