Below is the snippet for list the google search list options using both Selenium RC and web driver
package selenium;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import com.thoughtworks.selenium.Selenium;
public class GoogleTests {
static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
driver = new FirefoxDriver();
driver.get("http://www.google.co.in");
driver.findElement(By.xpath("//input[@type='text']")).sendKeys("Selenium webdriver");
// Selenium Code to print all the options from the dropdown suggestionlist
Selenium selenium = new WebDriverBackedSelenium(driver,
"http://www.google.co.in");
Thread.sleep(2000);
System.out.println("Below list is printed by Selenium RC");
int listCount = selenium.getXpathCount(
"//table[@class='gssb_m']/tbody/tr").intValue();
for (int i = 1; i < listCount; i++) {
String listValues = selenium
.getText("//table[@class='gssb_m']/tbody/tr[" + i
+ "]/td[1]/div/table/tbody/tr[1]/td[1]/span");
System.out.println(listValues);
}
// Webdriver Code to print all the options from the dropdown suggestion list
System.out.println("Below list is printed by Webdriver");
List<WebElement> options = driver.findElements(By
.xpath("//table[@class='gssb_m']/tbody/tr"));
for (WebElement webElement : options) {
System.out.println(webElement.getText());
}
// driver
driver.close();
driver.quit();
}
}
package selenium;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import com.thoughtworks.selenium.Selenium;
public class GoogleTests {
static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
driver = new FirefoxDriver();
driver.get("http://www.google.co.in");
driver.findElement(By.xpath("//input[@type='text']")).sendKeys("Selenium webdriver");
// Selenium Code to print all the options from the dropdown suggestionlist
Selenium selenium = new WebDriverBackedSelenium(driver,
"http://www.google.co.in");
Thread.sleep(2000);
System.out.println("Below list is printed by Selenium RC");
int listCount = selenium.getXpathCount(
"//table[@class='gssb_m']/tbody/tr").intValue();
for (int i = 1; i < listCount; i++) {
String listValues = selenium
.getText("//table[@class='gssb_m']/tbody/tr[" + i
+ "]/td[1]/div/table/tbody/tr[1]/td[1]/span");
System.out.println(listValues);
}
// Webdriver Code to print all the options from the dropdown suggestion list
System.out.println("Below list is printed by Webdriver");
List<WebElement> options = driver.findElements(By
.xpath("//table[@class='gssb_m']/tbody/tr"));
for (WebElement webElement : options) {
System.out.println(webElement.getText());
}
// driver
driver.close();
driver.quit();
}
}
Fantastic article ! You havemade some very astute statements and I appreciate the the effort you have put into your
ReplyDeletewriting. Its clear that you know what you are writing about. I am excited to read more of your sites content.
Microsoft dynamics training
Hi Kiran,
ReplyDeleteWe have requirement like as soon as the application build deployment has been done, need to execute my jenkin jobs automatically.
Please suggest me on this.
Thanks,
Mahesh
Hi Mahesh, You can do the Jenkins integration by having your Selenium Project with Ant/Maven and its just few steps to setup Jenkins
Delete