Tuesday, June 17, 2014

Select value from dropdown without using Select class. Good example.

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class SelectFromDropDown {
    public static void main(String[] args) {
        WebDriver driver=new FirefoxDriver();
        driver.get("https://makemytrip.com/");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.findElement(By.xpath("//div//span[2][contains(@class,'flL travelers rght_space room_sec1')]")).click();
        Actions act = new Actions(driver);
        act.sendKeys(Keys.chord(Keys.DOWN,Keys.DOWN)).perform(); //press down key two times to select 3
    }
}

2 comments:

  1. Hi Sanjay,
    I am trying to login into makemytrip.com. Below is the code which I have written, The control goes and writes the username, but the password is not getting populated even though the id of the field is proper. It throws element is currently not visible error. but on intervention, if I click on the password field, the password is entered and the login will be successful. Please help

    public static void successlogin(){

    WebDriver d=new FirefoxDriver();
    d.manage().window().maximize();
    d.get("http://www.makemytrip.com");
    d.findElement(By.id("login_dropOpenItem")).click();
    d.findElement(By.id("username")).sendKeys("rishi.vg1@gmail.com");
    /*Actions act=new Actions(d);
    act.sendKeys(Keys.TAB);*/
    d.findElement(By.id("password")).sendKeys("somepassword");
    d.findElement(By.id("login_btn")).click();

    }

    ReplyDelete
    Replies
    1. The id which you use for Password field is in the HTML code. If you see the html code properly you will find that above id is in dull color. So that's why its not finding that id and giving exception. Please find the correct code here-

      http://selenium-makeiteasy.blogspot.in/2014/06/login-to-httpwwwmakemytripcom.html

      Delete