Saturday 18 October 2014

How to print all option values from drop down box in Selenium in Java?

Below code shows how we can print the text of all options in drop down box in Selenium in Java.

package seleniumtest;

//import the required classes
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.Select;

public  class ElementIdentification {
 
 public static void main(String[] args) {
  
     WebDriver driver =null;
     //set the driver path
     System.setProperty("webdriver.chrome.driver", "F:\\selenium\\csharp\\chromedriver.exe");
    
     System.setProperty("webdriver.ie.driver", "F:\\selenium\\IEDriverServer_Win32_2.43.0\\IEDriverServer.exe");
    
  
    
     driver = new InternetExplorerDriver();
    
     //set the timeouts for page load and all elements
     driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
     driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
     
    
      try{
       
       //open the given web page
   
    driver.get("http://register.rediff.com/commonreg/index.php");
       //driver.get("file:///F:/selenium/selenium-blog.html");
        
    //Maximise the browser window
    driver.manage().window().maximize();
    
    //System.out.println(driver.getPageSource());
    
    System.out.println(driver.getTitle());
    
    
    System.out.println(driver.getCurrentUrl());
    
    WebElement y = driver.findElement(By.id("date_day"));
      
   
    
    Select p = new Select(y);
    p.selectByVisibleText("11");
    String z = p.getFirstSelectedOption().getAttribute("value");
    System.out.println(z);
    
    List <WebElement> k = p.getOptions();

    //Code to print the text values of the options in list box
    for(int i=0; i<k.size();i++)
     System.out.println(k.get(i).getText());
    
      

    
    //wait for 2 seconds
    Thread.sleep(2000);
   }catch(Exception e){
   
   //print exception if any
   System.out.println(e.getMessage() );
   e.printStackTrace();
  }
   finally{
   
   //close the driver
   driver.close();
   //quit the driver.
   driver.quit();
  } 
 }
}


What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

How to generate unique random numbers in Selenium in Java?

In software testing projects, we usually need to enter the test data such that data is unique. So you may use below code to create such random unique numbers in Selenium.

//Create a date object
Date d1 = new Date( );

//Create formatting object
SimpleDateFormat formatting = new SimpleDateFormat ("yyyyMMddhhmmss");

//Create unique numbers 
System.out.println("Unique Number is " + formatting.format(d1));

In above code, we have used system date to create unique numbers. By changing the

What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

Saturday 11 October 2014

Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones

You may get below error message when working with Selenium Webdriver and Internet Explorer browser.

Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 1.43 seconds

To fix above error you need to make below setting in Internet Option -> Security Tab as shown in below image.

Enable Protected Mode must be set to the same value (enabled or disabled) for all zones


If you have no permission to modify above settings, you can do it using below code.

DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.
                 INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
WebDriver driver = new InternetExplorerDriver(capabilities);

This will launch Internet explorer such that Enable Protected Mode is set to the same value (enabled or disabled) for all zones.

What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

The method sendKeys(CharSequence[]) in the type WebElement is not applicable for the arguments (String)

You may get below error while working with selenium.

The method sendKeys(CharSequence[]) in the type WebElement is not applicable for the arguments (String)

To fix this error, you will have to change the compiler compliance level of the project to JRE 1.6 or above in Eclipse.
To open below window, you will have to right click on the Java Project folder you are working on.


I was getting the sendKeys error when I was using JRE 1.4 compliance level. After changing the compiler compliance level to 1.6, the error was resolved.

What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

Buy Best Selenium Books

Contributors