Showing posts with label Selenium. Show all posts
Showing posts with label Selenium. Show all posts

Saturday, June 15, 2013

Selecting Multiple Option at a time from a List

One of the Scenario that is less frequent but comes in picture sometime is when we have to select
a Multiple options from a list Box  e.g. you are applying for a job and you have to select Domain in which you are working such as Finance , Retail etc . The below screen will give you an idea



So lets start the Coding for the same . This can be done very easily using the AdvancedUserInteractions
You can read a lot about them Here
We will be using some KeyBoard Actions  ( We have to hold CONTROL key and then Click on options which we want to select ) , so the same can  be done using ActionBuilders in webdriver.Lets take a look at
the sample code for the same scenario

        Actions builder = new Actions(driver);
        WebElement option1 = driver.findElement(By.id("m_opt_0"));

        WebElement option2 =driver.findElement(By.id("m_opt_3"));
        builder.keyDown(Keys.CONTROL).click(option1).click(option2).keyUp(Keys.CONTROL);
        Action select_multiple = builder.build();
        select_multiple.perform();

what the Hell above code is doing ??

1) Its saving both the Options you want to select in a webElement Object .
2) Using Actionbuilder its Pressing CONTROL Key , and then Clicking option1 object and subsequently Option2 element .

Hope that Helped.






Thursday, June 13, 2013

Switching from one frame to Another

One of the Classic Scenario that is faced by newbies in Selenium ( yes, i am a newbie too.. ) is that IDE locates the element when clicked and Highlighted but when Code in Java ( i love java ..u can choose any language you are good into) is ran it shows error element not found ...blah blah.


This is because there is an iframe in which your element is located and you are trying to locate that without switching the frame.

Simple Step to do this is as follow :-

WebElement i_frame = driver.findElement(By.xpath("/html/body/div/div[2]/div/div/iframe"));
// you can find the element iFrame by any other suitable method like id , css  locator etc .
driver.switchTo().frame(i_frame);  // switching is done ..now happy execution....

See below pic whichh shows presence of Iframe in HTML source code.

Handling Elements of a Webtable

The Code for Accessing a Cell value of a webtable would be like this. 

 driver.navigate().to(" Site which you want to recordl");
 WebElement table = driver.findElement(By.xpath("/html/body/div/div[4]/div/div/div[2]/div[2]/div[3]/table/tbody"));   // This would be the Xpath for locating the Element table in which tr and td exists
Collection<WebElement> table_Row_Object = table.findElements(By.tagName("tr"));
// The collection of Web Elements is collection all the WebElements with Tags tr in it. This is for table Object //only
WebElement table_Cell , table_Row ;
Collection<WebElement> tr_collection;
String Value_Cell;
 for ( Iterator<WebElement> i = table_Row_Object.iterator();i.hasNext(); )
        {
         table_Row = i.next();  // First Weblelement of tr is assigned
         tr_collection = table_Row.findElements(By.tagName("td")); //  Find all td in tr webelement .
         for ( Iterator<WebElement> j = tr_collection.iterator(); j.hasNext();)
         {
             table_Cell = j.next();
             Value_Cell = table_Cell.getText();
             System.out.println(Value_Cell); // And we won.... :)
          }
   }