What method should i use to display the country selected from dropdown on console in Selenium Webdriver?



  • http://demo.guru99.com/test/newtours/register.php

    For the above site, I am writing a Selenium code to select a country from the drop-down and want to display selected country on the console in eclipse. What method should I use to display selected country INDIA on console?

    Below is my code:

    package ayushi;
    import java.util.List;
    import java.util.concurrent.TimeUnit;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.support.ui.Select;
    
    public class Dropdown {
    
        public static void main(String[] args) throws InterruptedException {
           System.setProperty("webdriver.chrome.driver", "D:\\Selenium Setup\\chromedriver_win32\\chromedriver.exe");
           WebDriver x=new ChromeDriver();
           x.get("http://demo.guru99.com/test/newtours/register.php");
           x.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
           Thread.sleep(3000);
    
           System.out.println("CONTACT INFORMATION");
           WebElement element1=x.findElement(By.name("firstName"));
           element1.sendKeys("Ayushi");
           String a=element1.getAttribute("value");
           System.out.println("First Name: "+a);
           Thread.sleep(1000);
    
           WebElement element2=x.findElement(By.name("lastName"));
           element2.sendKeys("Sharma");
           String b=element2.getAttribute("value");
           System.out.println("Last Name: "+b);
           Thread.sleep(1000);
    
           WebElement element3=x.findElement(By.name("phone"));
           element3.sendKeys("9911554411");
           String c=element3.getAttribute("value");
           System.out.println("Phone: "+c);
           Thread.sleep(1000);
    
           WebElement element4=x.findElement(By.name("userName"));
           element4.sendKeys("ayushi.sharma@suretekinfosoft.com");
           String d=element4.getAttribute("value");
           System.out.println("Email: "+d);
    
           System.out.println("----------------------------------");
    
           System.out.println("MAILING INFORMATION");
           WebElement element5=x.findElement(By.name("address1"));
           element5.sendKeys("Pratap Nagar Near Police Lines Ghaziabad");
           String e=element5.getAttribute("value");
           System.out.println("Address: " +e);
    
           Thread.sleep(1000);
           WebElement element6=x.findElement(By.name("city"));
           element6.sendKeys("Ghaziabad");
           String f=element6.getAttribute("value");
           System.out.println("City: "+f);
           Thread.sleep(1000);
    
           WebElement element7=x.findElement(By.name("state"));
           element7.sendKeys("Uttar Pradesh");
           String g=element7.getAttribute("value");
           System.out.println("State/Province: "+g);
           Thread.sleep(1000);
    
           WebElement element8=x.findElement(By.name("postalCode"));
           element8.sendKeys("201002");
           String h=element8.getAttribute("value");
           System.out.println("Postal Code: "+h);
           Thread.sleep(1000);
    
           Select drpcountry=new Select(x.findElement(By.name("country")));
           List<WebElement> el=drpcountry.getOptions();
           int iListsize=el.size();
           System.out.println("Total no. of Countries: "+iListsize);
           for(int i=0;i<iListsize;i++)
           {
               String cntry=drpcountry.getOptions().get(i).getText();
           System.out.println("Country "+(i+1)+" :"+cntry);
           }
           drpcountry.selectByVisibleText("INDIA");
           Thread.sleep(1000);
    
           System.out.println("----------------------------------");
    
           System.out.println("USER INFORMATION");
           WebElement element9=x.findElement(By.name("email"));
           element9.sendKeys("ayushi");
           String i=element9.getAttribute("value");
           System.out.println("User Name: "+i);
    
           Thread.sleep(1000);
           WebElement element10=x.findElement(By.name("password"));
           element10.sendKeys("ayushi17");
           String j=element10.getAttribute("value");
           System.out.println("Password: "+j);
           Thread.sleep(1000);
    
           WebElement element11=x.findElement(By.name("confirmPassword"));
           element11.sendKeys("ayushi17");
           String k=element11.getAttribute("value");
           System.out.println("Confirm Password: "+k);
           Thread.sleep(2000);
    
           //Click Submit button
           x.findElement(By.name("submit")).click();
           System.out.println("Registration form is submitted successfully");
        }
    }
    


  • You can get the selected value from dropdown using getFirstSelectedOption() and getText() methods.

    System.out.println("Selected country: " + drpcountry.getFirstSelectedOption().getText());
    

    For reference: http://seleniumtwo-by-arun.blogspot.com/2013/07/144-using-getfirstselectedoption-with.html



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2