1

I am testing www.paytm.com

In that I have filled in the mobile number but I am not able to focus and select the mobile operator which is present in the dropdown just after the phone number section.

I want to select 'vodafone' as the value from the dropdown.

How to do that using webdriver

Code I have written till now

`

public class MMT {

 public static void main (String[] args)
 {

System.setProperty("webdriver.chrome.driver","E:/som/chromedriver/chromedriver.exe");

WebDriver driver = new ChromeDriver();

driver.get("http://paytm.com");

driver.findElement(By.id("mobile-number")).sendKeys("9876543212");`

What should be my next steps?

3 Answers 3

5

Please Use select.selectByValue("Vodafone"); OR kindly post the HTML code and go through this Link Link

As per your comments below updated the code:

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class Test1 {
    public static WebDriver driver = new FirefoxDriver();
    public static void main(String[] args) {
            driver.get("http://www.paytm.com");
            driver.manage().window().maximize();
               driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
              driver.findElement(By.id("mobile-number")).sendKeys("9876543212");
                driver.findElement(By.xpath("(//button[@type='button'])[2]")).click();
                driver.findElement(By.xpath(".//*[@id='mobile-oprator-dd']/div/div[1]/div/ul/li[18]/a/span")).click();
                driver.findElement(By.id("mobile-amount")).sendKeys("100");
            Select select = new Select(driver.findElement(By.id("mobile-operator")));
            select.selectByValue("Vodafone");**//Here you wanted to select "Vodafone"**
        }
    }
7
  • 1
    I think you are new to selenium webdriver, please go through this link docs.seleniumhq.org/docs/03_webdriver.jsp
    – QA4it
    Commented Sep 6, 2014 at 11:37
  • Can you please give me the whole command which I am supposed to write after the codes I have written?
    – Som Ghosh
    Commented Sep 6, 2014 at 11:49
  • 1
    Us this code please driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); driver.findElement(By.id("mobile-number")).sendKeys("9876543212"); driver.findElement(By.xpath("(//button[@type='button'])[2]")).click(); driver.findElement(By.xpath(".//*[@id='mobile-oprator-dd']/div/div[1]/div/ul/li[18]/a/span")).click(); driver.findElement(By.id("mobile-amount")).sendKeys("100"); Select select = new Select(driver.findElement(By.id("mobile-operator"))); select.selectByValue("Vodafone");
    – QA4it
    Commented Sep 6, 2014 at 12:21
  • Thanks for your help. I tried your code but I am getting this error Exception in thread "main" org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //*[@id='mobile-oprator-dd']/div/div[1]/div/ul/li[??18]/a/span because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//*[@id='mobile-oprator-dd']/div/div[1]/div/ul/li[??18]/a/span' is not a valid XPath expression.
    – Som Ghosh
    Commented Sep 8, 2014 at 5:06
  • @Som Ghosh I just verified and it was working fine. Please recheck.
    – QA4it
    Commented Sep 8, 2014 at 6:16
1

There are 3 ways from which you can select the value from the drop-down:

1) SelectByIndex

Select select = new Select(driver.findElement(By.id("mobile-operator")));
select.selectByValue(index of the record you want to select );

2) SelectByValue

Select select = new Select(driver.findElement(By.id("mobile-operator")));
select.selectByValue(value of the record you want to select );

3) SelectByVisibleText

Select select = new Select(driver.findElement(By.id("mobile-operator")));
select.selectByValue(visible text of the record you want to select );
0

Simply you have to use SelectByValue it is very easy to select the value from dropdown in case you want to select specified value as below.

Select sel = new Select(driver.findElementBy.xpath("//input[@data-reactid='184']")).SelectByValue("Airtel");

You should use exact name to select from the dropdown.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.