1

My intention is to select a value from drop down (From field) in RedBus site. And I am using Xpath to select it.

I am using the following code:

WebDriver driver=new FirefoxDriver();
        driver.get("http://www.redbus.in/");
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
        driver.findElement(By.id("DDLSource")).sendKeys("Chenn");
        driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
        driver.findElement(By.xpath(".//*[@id='123']")).click();

But it's not working. Only sending the values but not selecting.

Can anybody please help me out....

2
  • if you need to select the value from such drop downs then you need to first click on the textbox. This makes the dropdown visible and after that you need to perform click operation using an xpath for the city name. Let me know if you require more help. Commented Sep 25, 2013 at 8:54
  • I have exactly tried the same what you are saying.1. I have selected the text box and passed the values "chenn"2. Then it show me the option 3. But I could not choose a value from it.Can you please help me out Commented Sep 25, 2013 at 10:40

3 Answers 3

2

I have tested the following code in firefox and it works. If you want to select a city like chennai then just type c into the from textbox and you will get a list of all the cities starting with c. Also this will enable the dropdown element to become visible. After this use the xpath and change the name of the city to select it in the dropdown. Hope this helps. Happy coding.

WebDriver driver = new FirefoxDriver();
        driver.get("http://www.redbus.in");
        driver.findElement(By.xpath("//input[@id = 'DDLSource']")).sendKeys("c");
 //Pass the city name like Chennai instead of Chakshu
            driver.findElement(By.xpath("//dl[@id = 'lis']//dt[text()='Chakshu']")).click(); 
Sign up to request clarification or add additional context in comments.

7 Comments

It works vinay.But can u please let me know "//dl[@id = 'lis']//dt[text()='Chakshu']" how you find this path? Or written the html code on your own.
This is the way xpath has to be written. Not sure if there is a website which can train you on this. Will keep you posted if I find a website for learning xpath.
okay I am new to selenium.But I am trying the same way to select To field with the xpath "driver.findElement(By.id("DDLDestination")).sendKeys("th");driver.findElement(By.xpath("//dl[@id = 'lis']//dt[text()='Theni']")).click();". But its not working. Can you help me?
Change the c to t. If you want to select a city then enter the first letter of the city in the line driver.findElement(By.xpath("//input[@id = 'DDLSource']")).sendKeys("c");
Can you try this?driver.findElement(By.id("DDLDestination")).sendKeys("t"); driver.findElement(By.xpath("//dl[@id = 'lis']//dt[text()='Theni']")).click(); Its not working for me to select a value in 'To' field
|
2

Try it with

Select selectBox = new Select(driver.findElement(By.id("DDLSource")));
selectBox.selectByVisibleText(aText);

1 Comment

But I have to send some keys to get the suggested optins. Please visit the site once for better understanding
2

In order to clicking on something on dropdown you have to use WebDriver function:

new Select(dropdownElement).selectByVisibleText(textValue);

dropdownElement is a WebElement, you can use there driver.findElementBy...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.