- Selenium WebDriver Other Concepts
Wednesday, April 17, 2013
What is the difference between WebDriver.close() and WebDriver.quit()
WebDriver.close() method closes the current window.
See the below example, here it opens the new link in a different window and upon executing the close() method, it closes the parent window and leaving the new window open.
WebDriver driver=new FirefoxDriver();
driver.get("http://google.com");
driver.manage().window().maximize();
WebElement oWE=driver.findElement(By.linkText("About Google"));
Actions oAction=new Actions(driver);
oAction.moveToElement(oWE);
oAction.contextClick(oWE).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
driver.close();
Whereas, WebDriver.quit() method quits the driver, and closing every associated window.
In the above example, if you use driver.quit() in place of close , then first opens "google" page and then open the "about google" in new window and closes both the windows.
See the below example, here it opens the new link in a different window and upon executing the close() method, it closes the parent window and leaving the new window open.
WebDriver driver=new FirefoxDriver();
driver.get("http://google.com");
driver.manage().window().maximize();
WebElement oWE=driver.findElement(By.linkText("About Google"));
Actions oAction=new Actions(driver);
oAction.moveToElement(oWE);
oAction.contextClick(oWE).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
driver.close();
Whereas, WebDriver.quit() method quits the driver, and closing every associated window.
In the above example, if you use driver.quit() in place of close , then first opens "google" page and then open the "about google" in new window and closes both the windows.
Tuesday, April 16, 2013
How to check the object existance
Here in the below code, i am checking for an object existence.
//Max_TimeOut variable holds the time in seconds for the maximum time to wait before the control flows to NoSuchElementException block.
int Max_TimeOut=60;
public boolean isObjExists(WebDriver driver,By locator)
{
//I am putting the code in try catch because if the object does not exist, it throws exception.
try
{
//Before throwing exception, it will wait for the Max_timeout specified
WebDriverWait wait=new WebDriverWait(driver,Max_TimeOut);
wait.until(ExpectedConditions.elementToBeClickable(locator));
//If the element found, then it returns true
return true;
}
catch(NoSuchElementException exception)
{
//If the element is not found, then it returns false
return false;
}
}
This function can be invoked in like below:
By locator=By.name("Email");
if(obj.isObjExists(driver, locator))
{
Reporter.log("Object exists");
WebElement uNameElement=driver.findElement(locator);
uNameElement.sendKeys("abcd");
}
else
{
Reporter.log("Object does not exist");
}
//Max_TimeOut variable holds the time in seconds for the maximum time to wait before the control flows to NoSuchElementException block.
int Max_TimeOut=60;
public boolean isObjExists(WebDriver driver,By locator)
{
//I am putting the code in try catch because if the object does not exist, it throws exception.
try
{
//Before throwing exception, it will wait for the Max_timeout specified
WebDriverWait wait=new WebDriverWait(driver,Max_TimeOut);
wait.until(ExpectedConditions.elementToBeClickable(locator));
//If the element found, then it returns true
return true;
}
catch(NoSuchElementException exception)
{
//If the element is not found, then it returns false
return false;
}
}
This function can be invoked in like below:
By locator=By.name("Email");
if(obj.isObjExists(driver, locator))
{
Reporter.log("Object exists");
WebElement uNameElement=driver.findElement(locator);
uNameElement.sendKeys("abcd");
}
else
{
Reporter.log("Object does not exist");
}
Monday, March 18, 2013
How to choose Ext JS Combo values using Selenium WebDriver
When we see Ext JS Combo box, it looks like a ordinary combo box and when we try
List<WebElement> oListItems=oSel.getOptions();
It throws an error message saying,
Element should have been "select" but was "input"
The reason being Ext JS combo box are not just combo boxes, they combination of controls like,
<Input> and <Image> or <em>
<input> and <Select>
You can see the sample combo box object in Naukri.com website, which is attached
So inorder to select these items, first we need to click on the Input object and then select the value.
Following is the code for printing out all the values, you can use whatever function you want after clicking the object.
public static void main(String[] args)
for(int i=1;i<=oListItems.size()-1;i++)
Select oSel=
new Select(oCategoryItems);It throws an error message saying,
Element should have been "select" but was "input"
The reason being Ext JS combo box are not just combo boxes, they combination of controls like,
<Input> and <Image> or <em>
<input> and <Select>
You can see the sample combo box object in Naukri.com website, which is attached
So inorder to select these items, first we need to click on the Input object and then select the value.
Following is the code for printing out all the values, you can use whatever function you want after clicking the object.
import
java.util.List;
import
org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.WebElement;
import
org.openqa.selenium.firefox.FirefoxDriver;
import
org.openqa.selenium.support.ui.Select;
public
class PrintListBoxValues
{
{
WebDriver driver=
new FirefoxDriver();
driver.get(
"http://www.naukri.com/");
WebElement oCategory=driver.findElement(By.id(
"farea"));
oCategory.click();
WebElement oCategoryItems=driver.findElement(By.id(
"fareaSL"));
Select oSel=
new Select(oCategoryItems);
List<WebElement> oListItems=oSel.getOptions();
{
System.
out.println(oListItems.get(i).getText());
}
}
}Sunday, March 17, 2013
How to switch between different windows using Selenium WebDriver
Inorder to switch between Windows we should be knowing the window handlers and traverse between windows.
For that i am opening the link in a new window using clicking down button, after that moving to the specified window.
Here is the code:
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class MoveBetweenTabs
{
public static void main(String[] args)
{
WebDriver driver=new FirefoxDriver();
driver.navigate().to("http://www.google.com");
driver.manage().window().maximize();
WebElement oWE=driver.findElement(By.linkText("About Google"));
Actions oAction=new Actions(driver);
oAction.moveToElement(oWE);
oAction.contextClick(oWE).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
Set<String> sHandlers= driver.getWindowHandles();
for(String sHandler:sHandlers)
{
if(driver.switchTo().window(sHandler).getTitle().equals("Google"))
{
driver.switchTo().window(sHandler);
WebElement oWE1=driver.findElement(By.linkText("+Google"));
oWE1.click();
}
}
}
}
For that i am opening the link in a new window using clicking down button, after that moving to the specified window.
Here is the code:
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class MoveBetweenTabs
{
public static void main(String[] args)
{
WebDriver driver=new FirefoxDriver();
driver.navigate().to("http://www.google.com");
driver.manage().window().maximize();
WebElement oWE=driver.findElement(By.linkText("About Google"));
Actions oAction=new Actions(driver);
oAction.moveToElement(oWE);
oAction.contextClick(oWE).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
Set<String> sHandlers= driver.getWindowHandles();
for(String sHandler:sHandlers)
{
if(driver.switchTo().window(sHandler).getTitle().equals("Google"))
{
driver.switchTo().window(sHandler);
WebElement oWE1=driver.findElement(By.linkText("+Google"));
oWE1.click();
}
}
}
}
How to right click and choose an option using Selenium WebDriver
There is no direct way to choose an option after right clicking using Selenium WebDriver.
For Ex: what i mean here is say you open google.com
and then right click on "About Google" and have to choose "Open Link in new Tab"
In Selenium WebDriver there is no direct way to do this.
The work around is clicking {DOWN} button. But there is a disadvantage in this approach, suppose if your options dynamically change then this approach wont work.
Here is the sample code for the above approach:
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class RightClickAndChooseAnOption
{
public static void main(String[] args)
{
WebDriver driver=new FirefoxDriver();
driver.navigate().to("http://www.google.com");
driver.manage().window().maximize();
WebElement oWE=driver.findElement(By.linkText("About Google"));
Actions oAction=new Actions(driver);
oAction.moveToElement(oWE);
oAction.contextClick(oWE).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
}
}
For Ex: what i mean here is say you open google.com
and then right click on "About Google" and have to choose "Open Link in new Tab"
In Selenium WebDriver there is no direct way to do this.
The work around is clicking {DOWN} button. But there is a disadvantage in this approach, suppose if your options dynamically change then this approach wont work.
Here is the sample code for the above approach:
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class RightClickAndChooseAnOption
{
public static void main(String[] args)
{
WebDriver driver=new FirefoxDriver();
driver.navigate().to("http://www.google.com");
driver.manage().window().maximize();
WebElement oWE=driver.findElement(By.linkText("About Google"));
Actions oAction=new Actions(driver);
oAction.moveToElement(oWE);
oAction.contextClick(oWE).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
}
}
Wednesday, February 13, 2013
How to execute JavaScript using Selenium WebDriver
With Java Script we can access the DOM Properties. By doing so we can get the properties values of those objects.
Say i have a Div or a Span control like below:
<Div id="SampleDiv1">I am in Div</Div> or
<Span id="SampleSpan1">I am in Span</Div>
Here we have two solutions to get the value of the objects.
Solution 1: Use the getAttribute method.
This method is described in below location:
http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebElement.html#getAttribute%28java.lang.String%29
So for ex above, we can use like:
String sReturnText=driver.findElement(By.id("SampleSpan1")).getAttribute("innerHTML");
System.out.println(sReturnText);
Because in the above example we are trying to get text from the control we can directly use getText method like below:
String sReturnText=driver.findElement(By.id("SampleSpan1")).getText();
System.out.println(sReturnText);
Solution 2: We can get the DOM properties be JavaScript
There might be situations you will be needing to run the JavaScript in your automation. To do this/cover the example follow below steps:
String sJScript = "return document.getElementById('SampleSpan1').innerHTML;";
String sReturnText = (String) ((JavascriptExecutor) driver).executeScript(sJScript);
System.out.println(sReturnText);
Tuesday, February 12, 2013
How to close a dialog box using Selenium WebDriver
There might be places, when you click on an object a dialog will pop up asking your conformation. How to close those kind of alert messages?
The below code help you to close any such alert boxes.
import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class PopupDialog
{
public static void main(String[] args)
{
WebDriver driver=new FirefoxDriver();
driver.get("http://www.agoda.com/?ymsg=1&tick=634955861691");
Alert alt=driver.switchTo().alert();
alt.accept();
}
}
The below code help you to close any such alert boxes.
import org.openqa.selenium.Alert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class PopupDialog
{
public static void main(String[] args)
{
WebDriver driver=new FirefoxDriver();
driver.get("http://www.agoda.com/?ymsg=1&tick=634955861691");
Alert alt=driver.switchTo().alert();
alt.accept();
}
}
Saturday, February 9, 2013
How to work with Cookies in a web site using selenium webdriver
import java.util.Set;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FindAllCookiesInaWebSite
{
public static void main(String[] args)
{
WebDriver driver=new FirefoxDriver();
driver.get("http://in.yahoo.com/");
Set<Cookie> cookies=driver.manage().getCookies();
//To find the number of cookies used by this site
System.out.println("Number of cookies in this site "+cookies.size());
for(Cookie cookie:cookies)
{
System.out.println(cookie.getName()+" "+cookie.getValue());
//This will delete cookie By Name
//driver.manage().deleteCookieNamed(cookie.getName());
//This will delete the cookie
//driver.manage().deleteCookie(cookie);
}
//This will delete all cookies.
//driver.manage().deleteAllCookies();
}
}
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FindAllCookiesInaWebSite
{
public static void main(String[] args)
{
WebDriver driver=new FirefoxDriver();
driver.get("http://in.yahoo.com/");
Set<Cookie> cookies=driver.manage().getCookies();
//To find the number of cookies used by this site
System.out.println("Number of cookies in this site "+cookies.size());
for(Cookie cookie:cookies)
{
System.out.println(cookie.getName()+" "+cookie.getValue());
//This will delete cookie By Name
//driver.manage().deleteCookieNamed(cookie.getName());
//This will delete the cookie
//driver.manage().deleteCookie(cookie);
}
//This will delete all cookies.
//driver.manage().deleteAllCookies();
}
}
Friday, December 21, 2012
How to click on a element with specific Text
How we will click on a element(Link/button/any element) with a specific Text.
In the below example i open Yahoo Website and look for an element called "Mail", if it found it clicks on the link else it reports Exception saying String not found.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class ClickLinkByText
{
public static void main(String[] args)
{
String s_URL="http://www.yahoo.com";
String s_SearchLink="Mail";
WebDriver driver=new FirefoxDriver();
driver.get(s_URL);
try
{
driver.findElement(By.xpath("//*[@title='"+s_SearchLink+"']")).click();
}
catch(Exception e)
{
System.out.println("Searched string not found");
};
}
}
In the below example i open Yahoo Website and look for an element called "Mail", if it found it clicks on the link else it reports Exception saying String not found.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class ClickLinkByText
{
public static void main(String[] args)
{
String s_URL="http://www.yahoo.com";
String s_SearchLink="Mail";
WebDriver driver=new FirefoxDriver();
driver.get(s_URL);
try
{
driver.findElement(By.xpath("//*[@title='"+s_SearchLink+"']")).click();
}
catch(Exception e)
{
System.out.println("Searched string not found");
};
}
}
Wednesday, November 28, 2012
How to read and write content from/to a text file using Selenium WebDriver
There are many ways you can do this.If you wanted to read the content line by line level you can BufferReader/BufferWriter and the below script will helps you.
import java.io.*;
public class FileHandling
{
public static void main(String[] args)
{
File o_File1=new File("C:\\Input1.txt");
File o_File2=new File("C:\\Input2.txt");
String line;
try
{
BufferedReader o_br1=new BufferedReader(new FileReader(o_File1));
BufferedWriter o_bw1=null;
if(!o_File2.exists())
{
o_File2.createNewFile();
}
o_bw1 = new BufferedWriter(new FileWriter(o_File2) );
while((line=o_br1.readLine())!=null)
{
o_bw1.write(line);
}
o_bw1.close();
o_br1.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
}
import java.io.*;
public class FileHandling
{
public static void main(String[] args)
{
File o_File1=new File("C:\\Input1.txt");
File o_File2=new File("C:\\Input2.txt");
String line;
try
{
BufferedReader o_br1=new BufferedReader(new FileReader(o_File1));
BufferedWriter o_bw1=null;
if(!o_File2.exists())
{
o_File2.createNewFile();
}
o_bw1 = new BufferedWriter(new FileWriter(o_File2) );
while((line=o_br1.readLine())!=null)
{
o_bw1.write(line);
}
o_bw1.close();
o_br1.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
}
Friday, October 26, 2012
Synchronization or Waits in Selenium WebDriver
When our automation execution starts, then there should be fair communication between tool and application.
What i mean here is if the tool is too fast of execution and the application/objects are not fully loaded/not ready by that time, then our automation test cases will fail. So the tool should wait(appropriate) till the objects are present/ready in the application, so that communication/synchronization happens between the tool and application so the chances of our test cases will pass.
Synchronization or Waits can be done in two ways.
1. Explicit Waits
2. Implicit Waits
Explicit Waits:
This can be achieved in 2 ways.
Thread.sleep:
Thread.sleep waits the specified time irrespective of the object state.
Ex: Thread.sleep(30000);
Here the execution is halted for 30 Sec., even if the object you are looking exists in 10 sec. So here tool unnecessarily waits for 20 sec.
Execution wont wait after 30 sec.s even if the object does not available, so the chances of your Test fails.
WebDriverWait:
We can tell the tool to wait only till the Condition met. Once the condition is met, the tool proceed with the next step.
This can be done with WebDriverWait in conjunction with ExpectedConditions Class.
There are few methods supported in ExpectedConditions class to support synchronisation.
Here is the example:
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement o_element = wait.until(ExpectedConditions.elementToBeClickable(By.id("Object Id")));
Here the tool waits a maximum time of 30 Sec., if the object you are looking is displayed in 10 sec. then the execution proceeds with the next step afte 10 secs. rather than waiting for 30 secs.
If you dont want to include any methods in ExpectedConditions class, then you can use below code:
WebDriver driver = new FirefoxDriver();
driver.get("Your URL");
WebElement o_Element = (new WebDriverWait(driver, 30))
.until(new ExpectedCondition<WebElement>(){
@Override
public WebElement apply(WebDriver d) {
return d.findElement(By.id("Object Id"));
}});
Here WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully or wait for maximum of 30 sec.
Implicit Waits:
An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("Your URL");
WebElement o_Element = driver.findElement(By.id("Object Id"));
How to retrieve web table content using Selenium WebDriver
There are many ways we can do this,
but in below example i am following "tr","td" approach.
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://money.rediff.com/");
WebElement element=driver.findElement(By.id("allpage_links"));
List<WebElement> rowCollection=element.findElements(By.xpath("//*[@id='allpage_links']/tbody/tr"));
System.out.println("Numer of rows in this table: "+rowCollection.size());
//Here i_RowNum and i_ColNum, i am using to indicate Row and Column numbers. It may or may not be required in real-time Test Cases.
int i_RowNum=1;
for(WebElement rowElement:rowCollection)
{
List<WebElement> colCollection=rowElement.findElements(By.xpath("td"));
int i_ColNum=1;
for(WebElement colElement:colCollection)
{
System.out.println("Row "+i_RowNum+" Column "+i_ColNum+" Data "+colElement.getText());
i_ColNum=i_ColNum+1;
}
i_RowNum=i_RowNum+1;
}
driver.close();
but in below example i am following "tr","td" approach.
WebDriver driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://money.rediff.com/");
WebElement element=driver.findElement(By.id("allpage_links"));
List<WebElement> rowCollection=element.findElements(By.xpath("//*[@id='allpage_links']/tbody/tr"));
System.out.println("Numer of rows in this table: "+rowCollection.size());
//Here i_RowNum and i_ColNum, i am using to indicate Row and Column numbers. It may or may not be required in real-time Test Cases.
int i_RowNum=1;
for(WebElement rowElement:rowCollection)
{
List<WebElement> colCollection=rowElement.findElements(By.xpath("td"));
int i_ColNum=1;
for(WebElement colElement:colCollection)
{
System.out.println("Row "+i_RowNum+" Column "+i_ColNum+" Data "+colElement.getText());
i_ColNum=i_ColNum+1;
}
i_RowNum=i_RowNum+1;
}
driver.close();
How to retrieve a specifec cell value from a web table using Selenium WebDriver
Here in this example i covering how to retrieve a specific cell value from Web Table using Selenium WebDriver:
The below code works well if the Cell contains plain text than any other controls etc.
int rownum,colnum;
String s_xpath;
WebDriver driver=new FirefoxDriver();
driver.get("https://www.irctc.co.in/");
rownum=2;
colnum=1;
//Here i am framing the xpath with rownum and colnum
s_xpath="//*[@id='tabslinks']/tbody/tr["+rownum+"]/td["+colnum+"]";
//getText method retrieves the cell value.
System.out.println(driver.findElement(By.xpath(s_xpath)).getText());
The below code works well if the Cell contains plain text than any other controls etc.
int rownum,colnum;
String s_xpath;
WebDriver driver=new FirefoxDriver();
driver.get("https://www.irctc.co.in/");
rownum=2;
colnum=1;
//Here i am framing the xpath with rownum and colnum
s_xpath="//*[@id='tabslinks']/tbody/tr["+rownum+"]/td["+colnum+"]";
//getText method retrieves the cell value.
System.out.println(driver.findElement(By.xpath(s_xpath)).getText());
Thursday, October 25, 2012
How to take a screenshot of the window using Selenium WebDriver
Below code demonstrate about how to take a snapshot of the Desktop window using Selenium WebDriver:
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TakeAScreenShot
{
public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
String s_FilePath="c:\\screenshot.jpg";
driver.get("http://www.google.com/");
try
{
File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//Once we have the screenshot in our file 'srcFile' you can use all FileUtils methods like
FileUtils.copyFile(srcFile, new File(s_FilePath));
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TakeAScreenShot
{
public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
String s_FilePath="c:\\screenshot.jpg";
driver.get("http://www.google.com/");
try
{
File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//Once we have the screenshot in our file 'srcFile' you can use all FileUtils methods like
FileUtils.copyFile(srcFile, new File(s_FilePath));
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
How to select an item in Listbox using Selenium Webdriver
Below are my best ways to select a list item from a listbox:
Option 1:
//First find the Listbox element as WebElement.
WebElement o_Item= driver.findElement(By.id("uw_flight_origin_input_d"));
//Then select the list item from the WebElement
o_Item.findElement(By.xpath("//option[contains(text(),'" + nameYouWant + "')]")).click();
Option 2:
Select menu = new Select(driver.findElement(By.id("uw_flight_origin_input_d"))); menu.selectByVisibleText(nameYouWant);
A complete sample code is here:
import org.openqa.selenium.support.ui.Select;
WebDriver driver=new FirefoxDriver();
driver.get("http://www.expedia.co.in/");
String nameYouWant="Hyderabad";
WebElement o_Item= driver.findElement(By.id("uw_flight_origin_input_d")); o_Item.findElement(By.xpath("//option[contains(text(),'" + nameYouWant + "')]")).click();
/*Select menu = new Select(driver.findElement(By.id("uw_flight_origin_input_d"))); menu.selectByVisibleText(nameYouWant);*/
In the same way how to select a checkbox or radio button based on a value:
Here is a sample HTML code for an application
<html>
<head></head>
<body>
<form>
<Input type="radio" name="sex" value="male">Male<br>
<Input type="radio" name="sex" value="female">Female<br>
Likes :<br>
<input type="checkbox" name="likes" value="cars">Cars<br>
<input type="checkbox" name="likes" value="bikes">Bikes<br>
<input type="checkbox" name="likes" value="Movies">Movies<br>
</form>
</body>
</html>
WebDriver driver=new FirefoxDriver();
driver.get("FilePath);
driver.findElement(By.xpath("//input[@name='sex' and @value='female']")).click();
driver.findElement(By.xpath("//input[@name='likes' and @value='Movies']")).click();
Subscribe to:
Posts (Atom)